Need help to debug code

1 visualización (últimos 30 días)
Cassandra Soliman
Cassandra Soliman el 2 de Mzo. de 2020
Comentada: Rena Berman el 14 de Mayo de 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 comentario
Rena Berman
Rena Berman el 14 de Mayo de 2020
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 2 de Mzo. de 2020
Editada: KALYAN ACHARJYA el 2 de Mzo. de 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)

Categorías

Más información sobre Labels and Annotations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by