Help storing data from while loop

2 visualizaciones (últimos 30 días)
John Furman
John Furman el 28 de Feb. de 2020
Respondida: Walter Roberson el 28 de Feb. de 2020
I am working on an assignment where I need to save the iteration and the root value for each iteration under a single column matrix I am going to put my code underneath. Please let me know if you can help!
x = [0:0.1:2];
% Set bounds for graph
F = 5*sin(x/4)-1.3;
% Create function for graph
plot(x,F),grid
% Display function
[bracket,x] = ginput(2)
% Let user select two bounds from graph
x_lo = min(bracket)
x_up = max(bracket)
f = @(x) 5*sin(x/4) - 1.3
error = 10;
iter = 0
while error > 1e-4
iter = iter+1
tr = x_lo - f(x_lo)*(x_up-x_lo)/(f(x_up) - f(x_lo))
fr = f(tr)
if sign(fr) == sign(f(x_up))
x_up = tr
else
x_lo = tr
end
error = abs(fr);
end
root = tr

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Feb. de 2020
info_to_save(iter, :) = [iter, fr];
However, since you know that the iterations always increase by 1 each time, you do not need to store the iteration number:
info_to_save(iter) = fr;
And afterwards:
[(1:length(info_to_save)).', info_to_save(:)]

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by