How do I display my secant method iteration values in a table?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kevin Osborn
le 1 Oct 2021
Commenté : Kevin Osborn
le 1 Oct 2021
Here is my code for the secant method approximation of the function.
func = @(x) 2*exp(-2*x) + 4*sin(x) - 2*cos(2*x);
x1 = 2.5;
x2 = 2.4;
tol = 1e-9;
f1 = func(x1);
dx = inf;
iter = 0;
while abs(dx) > tol
iter = iter + 1;
f2 = func(x2);
dx = (x2 - x1)*f2/(f2 - f1);
x1 = x2;
f1 = f2;
x2 = x2 - dx;
end
x2
iter
It says that it takes 5 iterations to get to the root. I would like to be able to display each of these iterations with what value they approximate for the root.
Could someone please help me with the code that would be required to do that? Thank you.
0 commentaires
Réponse acceptée
Matt Tearle
le 1 Oct 2021
If you want to display things nicely, you can use fprintf to format things into text. If the primary concern is getting the values to display, you should save the information you need in each pass through the loop. Here's both:
...
dx = inf;
iter = 0;
% start a record of iteration info
dispdata = [iter;x1;x2];
while abs(dx) > tol
iter = iter + 1;
f2 = func(x2);
dx = (x2 - x1)*f2/(f2 - f1);
x1 = x2;
f1 = f2;
x2 = x2 - dx;
% add the latest info to the list
dispdata(:,iter+1) = [iter;x1;x2];
end
% ugly
disp(dispdata)
% pretty
fprintf(" k: x1 x2\n")
fprintf("----------------------\n")
fprintf("%2d: %7.4f %7.4f\n",dispdata)
If you want to see the iterations as they happen, you should do a nice fprintf inside the loop for each iteration instead.
4 commentaires
Matt Tearle
le 1 Oct 2021
Again, it depends on whether you just want to plot everything at the end or add points as you go. Above, I showed how to save the iterations in dispdata - the first row is the iteration numbers, the second is x1, the third is x2. So you could just plot the first row against the third row:
plot(dispdata(1,:),dispdata(3,:),"o-")
You take row 3 - row 2 to get the dx values. Whatever you want.
If you want to add individual points to the plot as you go, that gets a little more complicated. You may not be familiar with the use of graphics objects. But let's have some fun. First, before the loop, make a plot and save the output.
...
% start a record of iteration info
dispdata = [iter;x1;x2];
% make a plot
p = plot(iter,x2,"o-");
while abs(dx) > tol
iter = iter + 1;
...
If you've never done this before, try it in the Command Window and leave off the semicolon. What you get back from plot is a graphics object that holds the various properties of the plot. If you dig into those properties, you'll find that there are two important ones called XData and YData that currently hold the data values. When you do that in the script right before the loop, they will each contain a single value. You can add the new data to these inside the loop:
...
% add the latest info to the list
dispdata(:,iter+1) = [iter;x1;x2];
% add to the plot
p.XData = [p.XData iter];
p.YData = [p.YData x2];
drawnow
end
...
The drawnow command does what it says - it makes MATLAB update the plot so you can see it. You may even want to add something like pause(0.2) instead to slow the animation down.
There are other ways to hack this, like hold on + plot, but this is the right way :)
Kevin Osborn
le 1 Oct 2021
Thank you so much. I think I will just use the basic plot, I just wanted to see it graphically, nothing too fancy. It's nice to know how to get it to plot individual points as it runs though. I will probably use that in the future somewhere. Thanks again.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!