Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters. ERROR in plotting graph
2 次查看(过去 30 天)
显示 更早的评论
clear all
close all
clc
tol=input ('Enter TOLERANCE number:') ;
n=input('Enter ITERATION number:');
n=100;
f=@(x) (x+1-2*sin(pi*x));
a=0;
b=0.5;
if f(a) * f(b)>0
warning('ít is not applicable:')
elseif f(a)==0
fprintf('The root is %d', a)
elseif f(b)==0
fprintf('The root is %d', b)
end
pre=0;
for i=1:n
c=(a+b)/2;
if f(c)==0
fprintf('The root is: %d\n', c)
elseif f(c)*f(b)<0
a=c;
elseif f(c)*f(a)<0
b=c;
end
if abs(c-pre)<=tol
break;
end
pre=c;
end
fprintf('The root is %g with the %.3d tolerance',c,tol)
plot (f=@(x), f(XR_1), 'pre')
0 个评论
采纳的回答
Cris LaPierre
2021-5-10
Your plot syntax is wrong. You cannot define a variable (f) inside your plot command. Use the documentation to identify a valid syntax.
The variable XR_1 is not defined in your code, either.
I see you have defined pre as a variable, but are passing it in as a character array to the plot command in what should be the linespec input.
Since you are only plotting a single point, you will want to include a marker style in your linespec. Perhaps try replacing your plot command with this.
plot(c, f(c),'ro')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!