Need help to debug code

1 Ansicht (letzte 30 Tage)
Cassandra Soliman
Cassandra Soliman am 2 Mär. 2020
Kommentiert: Rena Berman am 14 Mai 2020
function [x1] = tutorial1(x0,nMax,tol)
x0 = 1.5;
nMax = 15;
tol = 1e-4;
% for loop acting continously for 15 iterations
for i = 1:nMax
fx0= (x0).^3-3.^(x0)+1;
differentialx0=3.*(x0.^2) - 3.^x0.*log(3);
%function f(x0)respectively
x1 = x0 -fx0./differentialx0;
if abs(x1-x0)<tol
break
end
x0 = x1;
fprintf('Iteration = %d, x0 = %.4f, x1 = %.4f, fx1 = %.4f\n',i,x0,x1);
end
% Sample output code for monitoring (this should be included in your loop structure.
return
  1 Kommentar
Rena Berman
Rena Berman am 14 Mai 2020
(Answers Dev) Restored edit

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 2 Mär. 2020
Bearbeitet: KALYAN ACHARJYA am 2 Mär. 2020
#Need help to debug code
There is no coding error.
function [x1]=tutorial1(x0,nMax,tol)
% for loop acting continously for 15 iterations
for i = 1:nMax
fx0= (x0).^3-3.^(x0)+1;
differentialx0=3.*(x0.^2) - 3.^x0.*log(3);
%function f(x0)respectively
x1 = x0 -fx0./differentialx0;
if abs(x1-x0)<tol
break
end
x0 = x1;
fprintf('Iteration = %d, x0 = %.4f, x1 = %.4f, fx1 = %.4f\n',i,x0,x1);
end
% Your code as per definition/requirements
end
Save the function as Matlab function file "tutorial1.m" and call it from command promt or a other main script. Please note during call you have to pass the input arguments, as specified in the function file.
>> y=tutorial1(1.5,15,1e-4)

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by