0

这是我的程序,它应该从用户那里获取起始 x 值、步进值和结束 x 值的输入,用户还输入他喜欢的 y 函数,然后应该将图形绘制成图形但我保留遇到错误,有什么想法吗?

clear all ; % clear memory
close all ; % close any open figures
drawnow ; % update screen now
clc ; % clear screen


display('** Welcome to Plotting Program **') ;
display(' ');

start=input('please enter starting x value:');
step=input('please enter ending x value:');
stop=input('please enter step value:');

y= double(input('please input your equation:'));

x=double(start:step:stop);


plot(x,y);
4

1 回答 1

1

您可以使用str2func()将字符串输入转换为函数句柄:

y = input('Input only the RHS of your equation as a function of ''x'' and enclose in '': ')
y = str2func(['@(x)' y]);
plot(x,y(x))

另请注意,这double(start:step:stop)不起作用,因为您正在将字符转换为它们的 ASCII 映射:

double('20')
ans =
    50    48

改为使用str2double(input('...'))

于 2013-05-13T22:54:03.670 回答