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

1 visualizzazione (ultimi 30 giorni)
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')

Risposta accettata

Cris LaPierre
Cris LaPierre il 10 Mag 2021
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')

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by