Secant Method approximation problem
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I faced a problem in solving the Secant method approximation problem .. i don't need the solution i need to fix my code .. i didn't use MATLAB before this is Math course (Numerical Method) approximate value x50 .. i used a code with accuracy 10^-2 i didn't found X50 .. the interval is [1; 2] the initial value is x0 = 1 and x1 = 2. the equ is x5
3 commentaires
John D'Errico
le 16 Déc 2015
Please learn to use the {} Code button when you post code. All you need to is select the entire block of code, then click on the button. Otherwise, your code is unreadable. I've fixed it for you this time.
Réponses (1)
John D'Errico
le 17 Déc 2015
Modifié(e) : John D'Errico
le 17 Déc 2015
Now that I can read your code...
A few comments. First of all, I see that you use the variable "old", yet you never define it, ANYWHERE. That is surely likely to cause a problem. :)
Next, I would strongly suggest more carefully naming your variables. You have two endpoints x0 and x1. Initially, those points are set to a and b respectively. Then you evaluate the function at those points, and call the function values fa and fb. BE CONSISTENT in your variable names. If you are not so, then your code will become impossible to debug. Variable names that are easy to follow make code easier to read. So the value of the function at x0 should logically be f0. likewise, use f1 at x1. Or, you can keep a and b as the two points, then use fa and fb. Nothing stops you from changing the value of variables that are passed into a function. This matters not so much in a short code, but one day, when your code for some problem runs to many lines, with MANY variables, you will so much appreciate it.
Next, you create a new point called new from the secant prediction, then you effectively replace x0 and x1 with x1 and new respectively.
I see a logical flaw in the above approach. We want to have two points that bracket the root. A bracket means that the function values have opposite signs. If that is true, then the next point will be somewhere in the middle of the two, and we need not fear divergence. If we do have a bracket on the root, then we want to maintain that bracket situation. If the points do not bracket the root, so both function values are positive or both are negative, then we should replace the worst of the two previous points with the newest one.
So, depending on the value of f(new), you might want to decide to choose the next pair of points as either {x0,new} or {new,x1}. A simple scheme the ignores the idea of bracketing the roots might be as below.
Oh, also, USE COMMENTS! MATLAB does not charge per comment line. They are free. But comments are notes to yourself to help you debug your code in the future, hints about what a block of code does. Also, use indentation and white space. These things make your code easier to read.
As it is, your code looks like a very good start. I'll offer the code below as an idea to base your efforts.
function sol=secant(fn,a,b,tol)
% Evaluate fn at the current pair of points
fa = feval(fn, a); fb = feval(fn, b);
% the current interval is always [a,b],
% with function values of [fa,fb] at the
% respective endpoints. Terminate iteration
% when the interval length is no more than tol.
while abs(a-b) > tol
new = b − fb ∗ (b − a)/(fb − fa);
fnew = feval(fn,new);
% decide which pair of points on which to
% base the next estimate
if abs(fa) < abs(fb)
% use [a,new], so b is replaced with new.
b = new; fb = fnew;
else
% use [b,new], so a is replaced with new.
a = new; fa = fnew;
end
end
end;
% return the final point
sol=new;
Personally, I don't like my code as I wrote it above as a very good scheme for rootfinding. It can easily diverge, or it might converge quite slowly, but then the basic secant method has flaws, as you should learn in future assignments.
There are things I would do to improve it. I would use an iteration counter and limit. If the iteration is diverging, you need to stop it at some point, otherwise you may find an essentially infinite loop. I would ensure that IF I have a bracket around a root, that I maintain the bracket in my choice of the next interval. I would test to see if the function value at one of the points was ever exactly zero, then I could terminate execution immediately. (This event would be rare of course.)
There are other things one should do, but writing high quality code that is stable and robust to all problems is often a difficult thing, visualizing every possible thing that may go wrong, and seeing that your code will handle all possible problems.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical 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!