24小时热门版块排行榜    

查看: 2435  |  回复: 9

zongshengl

木虫 (小有名气)

[求助] Matlab中如何调用M文件作图

在处理图像时遇到了一个问题,还望高人指点。
首先建立了一个M文件,存在工作目录下,内容为:

Function y=objfun(x)

y=x^2+5*x+20;

现在,用matlab做出y关于x的图像,请问如何调用M文件,命令是哪个?先谢谢!

[ Last edited by dbb627 on 2011-11-30 at 22:45 ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

An important thing in ones life is to have a dream and to achive it!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖


zongshengl(金币+10): 谢谢你的帮助 2011-12-01 10:37:04
命令窗口输入
fplot(@objfun,[-10 10])
或者
把函数改保存
Function y=objfun(x)

y=x.^2+5*x+20;

然后在命令窗口输入
x=-10:0.1:10;
plot(x,objfun(x))

[ Last edited by dbb627 on 2011-11-30 at 22:54 ]

» 本帖已获得的红花(最新10朵)

The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
2楼2011-11-30 22:48:21
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zongshengl

木虫 (小有名气)

送鲜花一朵
引用回帖:
2楼: Originally posted by dbb627 at 2011-11-30 22:48:21:
命令窗口输入
fplot(@objfun,[-10 10])
或者
把函数改保存
Function y=objfun(x)

y=x.^2+5*x+20;

然后在命令窗口输入
x=-10:0.1:10;
plot(x,objfun(x))
[ Last edited by dbb627 on 2011-11-30 at ...

谢谢!我刚试了,解决问题了,
但是现在我又调试了一个文件如下:

function y = two_min(x)
if x<=20
    y = -exp(-(x/20).^2);
else
    y = -exp(-1)+(x-20)*(x-22);
end

Save the file as two_min.m in a folder on the MATLAB path.

在命令窗口运行时

>> x=-10:0.1:30;
>> plot(x,two_min(x))

提示:

??? Undefined function or method 'two_min' for input arguments of type 'double'.

这是怎么回事?谢谢你
An important thing in ones life is to have a dream and to achive it!
3楼2011-11-30 23:06:21
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

你的two_min是否存在了当前工作目录下面,
function y = two_min(x)
if x<=20
    y = -exp(-(x/20).^2);
else
    y = -exp(-1)+(x-20).*(x-22);
end
The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
4楼2011-11-30 23:30:05
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

baby_wolf

新虫 (初入文坛)

【答案】应助回帖

引用回帖:
4楼: Originally posted by dbb627 at 2011-11-30 23:30:05:
你的two_min是否存在了当前工作目录下面,
function y = two_min(x)
if x<=20
    y = -exp(-(x/20).^2);
else
    y = -exp(-1)+(x-20).*(x-22);
end

这样写是错误的。写的让人感觉很怪。
如果x只有一个元素,没有任何问题。如果x是一个向量的话,返回值y就得不到希望的值。

看我这样写是不是可行:
CODE:
function y=two_min(x)
y=zeros(size(x));  %定义y与x尺寸相同
ind=(x<=20);      %返回一个logic型
y(ind)=-exp(-(x(ind)/20).^2);
ind=(x>20);
y(ind)= -exp(-1)+(x(ind)-20).*(x-22);

5楼2011-12-01 01:17:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

baby_wolf

新虫 (初入文坛)

【答案】应助回帖

[quote]5楼: Originally posted by baby_wolf at 2011-12-01 01:17:19:
这样写是错误的。写的让人感觉很怪。
如果x只有一个元素,没有任何问题。如果x是一个向量的话,返回值y就得不到希望的值。

看我这样写是不是可行:
CODE:
function y=two_min(x)
y=zeros(size(x));  %定 ... [/quote]


最后一句再改一下。
[code]y(ind)= -exp(-1)+(x(ind)-20).*(x(ind)-22);

6楼2011-12-01 01:18:41
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zongshengl

木虫 (小有名气)

引用回帖:
4楼: Originally posted by dbb627 at 2011-11-30 23:30:05:
你的two_min是否存在了当前工作目录下面,
function y = two_min(x)
if x<=20
    y = -exp(-(x/20).^2);
else
    y = -exp(-1)+(x-20).*(x-22);
end

首先对你的回答表示感谢!
two_min.m文件是在工作目录下。

又尝试了下,发现这样做出的图只是最后一个函数的图像
即y = -exp(-1)+(x-20).*(x-22);不能达到要求。

我用
function y=two_min(x)
y=zeros(size(x));  %定义y与x尺寸相同
ind=(x<=20);      %返回一个logic型
y(ind)=-exp(-(x(ind)/20).^2);
ind=(x>20);
y(ind)= -exp(-1)+(x(ind)-20).*(x(ind)-22);
这个解决了,谢谢你的无私帮助!
An important thing in ones life is to have a dream and to achive it!
7楼2011-12-01 10:26:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zongshengl

木虫 (小有名气)

引用回帖:
6楼: Originally posted by baby_wolf at 2011-12-01 01:18:41:
最后一句再改一下。
CODE:
y(ind)= -exp(-1)+(x(ind)-20).*(x(ind)-22);


谢谢!问题解决了

我还有个疑问:(1)是不是分段函数都这样写?
(2)如果不是分段函数是不是就不用这样写了?

非常感谢
An important thing in ones life is to have a dream and to achive it!
8楼2011-12-01 10:29:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

引用回帖:
8楼: Originally posted by zongshengl at 2011-12-01 10:29:51:
谢谢!问题解决了

我还有个疑问:(1)是不是分段函数都这样写?
(2)如果不是分段函数是不是就不用这样写了?

非常感谢

分段函数有很简单的写法,不用那么复杂
y=-exp(10.5*(x+0.5)) x<-0.5
y =exp(3.51*(x0.5))-2*exp(-3.51*(x+0.5))   (-0.5,0.5)
y=-exp(10.5*(x+0.5))    x>0.5
CODE:
x=-1:0.02:1;
y=-exp(10.5*(x+0.5)).*( x<-0.5 )+...
    (exp(3.51*(x+0.5))-2*exp(-3.51*(x+0.5))).*(x>-0.5&x<0.5)...
    -exp(10.5*(x+0.5)).*(x>0.5);
plot(x,y)
figure;semilogy(x,y)

The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
9楼2011-12-01 10:37:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zongshengl

木虫 (小有名气)

引用回帖:
9楼: Originally posted by dbb627 at 2011-12-01 10:37:16:
分段函数有很简单的写法,不用那么复杂
y=-exp(10.5*(x+0.5)) x<-0.5
y =exp(3.51*(x0.5))-2*exp(-3.51*(x+0.5))   (-0.5,0.5)
y=-exp(10.5*(x+0.5))    x>0.5

[code]
x=-1:0.02:1;
y=-exp(10.5 ...

谢谢
An important thing in ones life is to have a dream and to achive it!
10楼2011-12-01 21:44:15
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zongshengl 的主题更新
信息提示
请填处理意见