don't know the problem in my while loop
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
hamza kharbouch
el 1 de En. de 2022
Respondida: hamza kharbouch
el 1 de En. de 2022
there is also another error idk about of :
Array indices must be positive integers or logical values.
Error in sym/subsref (line 890)
R_tilde = builtin('subsref',L_tilde,Idx);
iteration=0;i=1;
x=zeros(100,1);
syms f(x);
f(x) = input('donner une function : f(x)= ');
e = input('donner une la valuer de erreur : e = ');
x(1) = input('donner la valeur de depart : x1 = ');
while ( e < abs(x(i+1)-x(i)) )
iteration = iteration +1 ;
x(i+1)=x(i)-f(x(i))/diff(f(x(i)));
i=i+1;
end
disp('la value de x apartir de e est', x(i));
disp(iteration);
0 comentarios
Respuesta aceptada
the cyclist
el 1 de En. de 2022
You initialize the value of i to be zero.
Then you try
x(i)
which is equivalent to
x(0)
which means you are trying to access the zeroth element of x. But MATLAB indexing begins at 1, not 0, so you get that error.
8 comentarios
the cyclist
el 1 de En. de 2022
OK. Is the following code equivalent to your example, and give the error you see?
I removed the input functions (because they won't work here), and I also used an anonymous function instead of the symbolic function (because I don't have that toolbox).
iteration=0;
i=1;
x=zeros(100,1);
% syms f(x);
% f(x) = input('donner une function : f(x)= ');
% e = input('donner une la valuer de erreur : e = ');
% x(1) = input('donner la valeur de depart : x1 = ');
f = @(x) x.^3 - 10;
e = 0.001;
x(1) = 3;
while ( e < abs(x(i+1)-x(i)) )
iteration = iteration +1 ;
x(i+1)=x(i)-f(x(i))/diff(f(x(i)));
i=i+1;
end
disp('la value de x apartir de e est', x(i));
disp(iteration);
Más respuestas (1)
Ver también
Categorías
Más información sobre Symbolic Math Toolbox 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!