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 次查看(过去 30 天)
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

采纳的回答

KSSV
KSSV 2023-2-22
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by