Filter löschen
Filter löschen

Can anyone tell me why I'm getting an error here? "xold=xnew;'' is giving me an error in this statement and I'm not sure why.

3 Ansichten (letzte 30 Tage)
clear all
close all
clc
% Problem 6.7
while (1)
fx=@(x) x.^3-6*x.^2+11*x-6.1;
% Using the Modified Secant Method
xold=3.5;
d=0.01;
% Iteration 1
xold=xnew;
xnew=xold-d*xold*fx(xold)/(fx(xold+d*xold)-fx(xold));
ea=abs((xnew-xold)/xnew)*100;
% Iteration 2
xold=xnew;
xnew=xold-d*xold*fx(xold)/(fx(xold+d*xold)-fx(xold));
ea=abs((xnew-xold)/xnew)*100;
% Iteration 3
xold=xnew;
xnew=xold-d*xold*fx(xold)/(fx(xold+d*xold)-fx(xold));
ea=abs((xnew-xold)/xnew)*100;
fprintf('The root using 3 iterations of Modified Secant Method is %8.4f with an approximate relative error of %6.3e\n',xnew,ea)
end

Akzeptierte Antwort

KSSV
KSSV am 22 Feb. 2023
These lines:
xold=3.5;
d=0.01;
% Iteration 1
xold=xnew;
xnew=xold-d*xold*fx(xold)/(fx(xold+d*xold)-fx(xold));
will obviously thrwo an error becuase xnew is a new variable and it is not defined. You need to either comment this line or you may need to move to the next line. After calculating xnew then save xnew to xold.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by