Trying to retrieve and display the initial value of an input

3 visualizaciones (últimos 30 días)
Ibrahim Ali
Ibrahim Ali el 26 de Sept. de 2021
Comentada: Ibrahim Ali el 27 de Sept. de 2021
From disp(q), I'm trying to show the interval chosen at the beginning from x0 to x1, the loop makes it so that x0 and x1 get changed, therefore I'm trying to specify a = the initial value of x0, and b = the initial value of x1, but Matlab gives an error:
Error in sekantH (line 3)
x0 = a;
Does anyone have a solution to this problem I'm having?
-------------------------------
function f = sekantH(f,x0,x1)
x0 = a;
x1 = b;
format long;
tol = 10^-10;
count = 0;
for i = 1:1000
x = x1-f(x1) * (x1-x0)/(f(x1)-f(x0));
count = count+1;
if abs(x1-x)<tol
break
end
x0 = x1;
x1 = x;
f = x;
count;
q = ['Number of iterations on the interval', ' [', num2str(a), ',' ,num2str(b), '] is = ', num2str(count), '. And the root is: '];
disp(q)
end

Respuesta aceptada

Dave B
Dave B el 26 de Sept. de 2021
Editada: Dave B el 26 de Sept. de 2021
I think you just flipped it, if you want to set a equal to the initial value of x0, do a=x0
demo(5,10)
a=5 b=10 x0=15 x1=30
function demo(x0,x1)
a = x0;
b = x1;
for i = 1:10
x0 = x0 + 1;
x1 = x1 + 2;
end
fprintf('a=%d b=%d x0=%d x1=%d\n',a,b,x0,x1)
end
  1 comentario
Ibrahim Ali
Ibrahim Ali el 27 de Sept. de 2021
You were correct, I had just flipped it! Works beautifully now, thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by