MATLAB Answers

Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters. ERROR in plotting graph

조회 수: 2(최근 30일)
Jim Wilson Owusu
Jim Wilson Owusu 2021년 5월 9일
댓글: Jim Wilson Owusu 2021년 5월 14일
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')

채택된 답변

Cris LaPierre
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개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by