How can i fix the Error at 1
Ältere Kommentare anzeigen
I cannot run my code because it says syntax error at "1" in line 2
f=@(x)+(x^3)-(x^1);
fd=@(1)+(3(x^2))+(x^-2));
a= round (input ('Enter Your Initial Value(x) :'),1);
tol = 0.0002; % 4 decimal places
fa = round(feval (f,a),1);
fda = round(feval (fd,a),1);
al = round((a-(fa/fda)),1);
error = round(abs((al-a)/al),1);
iter =0;
disp (' n Xn F(x) F(x) Xn+1 Error') disp (' -----------------------------------------------------------')
while (error>= tol)
iter = iter+1;
al = round(a-(fa/fda),1);
fal = round(feval(f,al),1);
fdal = round((feval(fd,al)),1);
error = round(abs(((al-a)/al)*100),1);
fprintf('%2i\t %f\t %f\t %f\t %f\t %f\t \n',iter,a,fa,fda,al,error)
if (error>=tol) a = al; fa = fal; fda = fdal;
end end
1 Kommentar
Antworten (2)
Walter Roberson
am 13 Mai 2020
fd=@(x) (1)+(3*(x.^2))+(x.^-2));
1 Kommentar
Amirah Sofia
am 13 Mai 2020
Rik
am 13 Mai 2020
You probably mean this instead:
fd=@(x)+(3*(x^2))+(x^-2);
% ^ ^ ^
% | add the * remove the extra parenthesis
% x instead of 1
On the other lines there are also several issues. Below is my attempt at debugging your code. It runs for a=100, although I can't tell if the output makes sense.
f=@(x) (x^3)-(x^1);
fd=@(x) (1)+(3*(x^2))+(x^-2);
a= round (input ('Enter Your Initial Value(x) :'),1);
tol = 0.0002; % 4 decimal places
fa = round(f(a),1);
fda = round(fd(a),1);
al = round((a-(fa/fda)),1);
error = round(abs((al-a)/al),1);
iter =0;
disp (' n Xn F(x) F(x) Xn+1 Error')
disp (' -----------------------------------------------------------')
while (error>= tol)
iter = iter+1;
al = round(a-(fa/fda),1);
fal = round(f(al),1);
fdal = round((fd(al)),1);
error = round(abs(((al-a)/al)*100),1);
fprintf('%2i\t %f\t %f\t %f\t %f\t %f\t \n',iter,a,fa,fda,al,error)
if (error>=tol)
a = al; fa = fal; fda = fdal;
end
end
4 Kommentare
Amirah Sofia
am 13 Mai 2020
Then you probably mean
f=@(x) 1 + (x.^3) - (x.^-1);
% ^ ^
% these dots are to enable array operations
And if fd is intended as the derivative of f, you should do remove the 1+ term:
fd=@(x) (3*x.^2) + (x.^-2);
Your code is also uncommented, so without looking up the Newton-Raphson method I can't tell in which step you have an error. Try to write the algorithm in comments first and then write the code for each step.
ROBBIE DEXTER UKAU
am 13 Mai 2020
but 1 is there because its the derivative of x what do you mean by removing the 1+?
Rik
am 13 Mai 2020
@Robbie, there is no x in the original function (only a cube and reciprocal), so why would there be a 1+ in the derivative?
Kategorien
Mehr zu Installing Products finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!