Absolute and Relative Approximate Errors for Secant Method in MATLAB

In summary: In fact, I think the metric that you gave is the most accurate version of the relative error of all the different ways we could define it when we do not know the true value.In summary, the conversation is about using the secant method in MATLAB to solve an equation and calculating the root and absolute relative approximate error at the end of each iteration. The m-file has been rearranged to solve an infinite loop problem and the error calculation has been adjusted. However, there is still an error in the calculation that needs to be fixed. The current metric used is abs(fnnew), but using err(iter) may be a more accurate measure of the relative error. The conversation also discusses the importance of properly defining and calculating the relative error in
  • #1
chronicals
36
0
I try to solve this equation with secant method in MATLAB.

fn=40*n^1.5-875*n+35000

my initial guess is n1=60; n2=68; I want to find root and absolute relative approximate
error at the end of each iteration. I have an infinite loop. Can you help me repair my file?
This is my m-file:

clc
clear
n1=60;
n2=68;
tol=1e-3;
err0=3;
iter=0;
fprintf('iteration n relative approximate error\n')
while err0>=tol
iter=iter+1;
fn1=40*(n1).^1.5-875*(n1)+35000;
fn2=40*(n2).^1.5-875*(n2)+35000;
nnew=n2-fn2*((n2-n1)/(fn2-fn1));
fnnew=40*(nnew).^1.5-875*(nnew)+35000;
err(iter)=(abs((nnew-n2)/nnew))*100;
fprintf('%2d %f %f\n',iter,nnew,err(iter))


if nnew>n1
n1=nnew;

else
n2=nnew;

end

end
nnew
iter
 
Last edited:
Physics news on Phys.org
  • #2
I rearranged my m-file and i solved infinite loop problem but i think i have mistaken at calculating absolute relative approximate error at the end of each iteration, i think this command is wrong:

err(iter)=(abs((nnew-n2)/nnew))*100;

How can i fix this error calculation problem?



My m-file:

clc
clear
n1=60;
n2=68;
tol=1e-5;
err0=3;
iter=0;
fprintf('iteration n relative approximate error\n')
while err0>=tol
iter=iter+1;
fn1=40*(n1).^1.5-875*(n1)+35000;
fn2=40*(n2).^1.5-875*(n2)+35000;
nnew=n2-fn2*((n2-n1)/(fn2-fn1));
fnnew=40*(nnew).^1.5-875*(nnew)+35000;
err(iter)=(abs((nnew-n2)/nnew))*100;
fprintf('%2d %f %f\n',iter,nnew,err(iter))
err0=abs(fnnew);


if nnew>n1
n1=nnew;

else
n2=nnew;

end

end
nnew
iter
 
Last edited:
  • #3
Why are using abs(fnnew) as your error, isn't it err(sayac) ? I would also remove the factor of 100 from the relative error unless it is your meaning to express it as a percent relative error. Trivial quibble but the nice thing about the relative error is that the base 10 log of it gives you an estimate on the number of digits it is accurate to in decimal. So by setting your tol to 1e-5, you are desiring to be accurate to at least five digits.
 
  • #4
Born2bwire said:
Why are using abs(fnnew) as your error, isn't it err(sayac) ? I would also remove the factor of 100 from the relative error unless it is your meaning to express it as a percent relative error. Trivial quibble but the nice thing about the relative error is that the base 10 log of it gives you an estimate on the number of digits it is accurate to in decimal. So by setting your tol to 1e-5, you are desiring to be accurate to at least five digits.

If I use err(iter), I have an infinite loop, so I use abs(fnnew).

These are my results:

iteration n relative approximate error
1 62.759758 8.349685
2 62.689966 8.470309
3 62.691698 0.002762
4 62.691697 0.000001

nnew =
62.6917

iter =
4

how can second iterations' relative approximate error be 8.470309. I think this m-file is calculating 'relative approximate error' wrongly. Please help me fix this error command: err(iter)=(abs((nnew-n2)/nnew))*100;
 
  • #5
It is 8.4 because you are scaling the relative error by 100. Like I said, if you do not scale then the log of the relative error is indicative of the number of digits of accuracy. Indeed, you note that the first two digits, 62, do not change as you converge. Thus, you started out with two digits of accuracy which would correlate to log(0.08).

Using err(iter) is the proper thing to do. Right now fnnew only works because it is the same as the absolute error since you are trying to find the zero. But if you were trying to converge to any number other than zero then you would never converge properly. Which brings us to the relative error. That is not the relative error because you do not know the true answer. It is a measure of the difference between your old and new values I think. This is still a reasonable metric to use though as long as we assume that you are always converging at a constant or increasing rate (this could allow us to use this metric to estimate the actual relative error but that is unnecessary for most applications). For example, I use this when I do semi-infinite integrations.

From your sample output, it should have converged in four iterations since the "error" is 1.0e-6. If you removed the scaling factor of 100 then this should happen. If it is not ending, then it is probably because the result is fluctuating back and forth around the true answer. If you increased your tolerance slightly this may allow you to achieve convergence. This can happen due to floating point errors, getting trapped around an incorrect guess, or due to deficiencies in the algorithm.

EDIT: Hmmm... probably should be a little more blunt. In this case, you should use abs(fnnew) because you know that the result should be zero and thus abs(fnnew) is the amount by which the result is off from zero. So you can find the residual. That is, you can't find the error in the x you wish to find (since obviously you do not know x apriori) but you can find the error in the f(x) that you want to achieve. This is not always feasible. Sometimes, like say with an integration, you do not have such a metric to use. So using the relative change to the result, like you found in err(), is often a valid metric. Though, as you have found, it may not always be perfectly reliable.
 
Last edited:

1. What is the difference between absolute and relative approximate errors in the secant method?

The absolute approximate error is the difference between the exact solution and the approximate solution, while the relative approximate error is the absolute approximate error divided by the exact solution. In other words, the absolute approximate error tells us how far off our approximation is from the true solution, while the relative approximate error gives us a percentage of how much our approximation differs from the exact solution.

2. How is the secant method used to solve equations in MATLAB?

The secant method is an iterative root-finding algorithm that can be used to solve equations in MATLAB. It works by using two initial guesses and calculating a new approximation based on the secant line between these two points. This process is repeated until the desired level of accuracy is achieved.

3. What is the role of the tolerance value in the secant method in MATLAB?

The tolerance value is used to determine when the secant method should stop iterating and return the final approximation. It represents the maximum acceptable error between the exact solution and the approximate solution. A smaller tolerance value will result in a more accurate approximation, but may require more iterations to achieve.

4. How does the choice of initial guesses affect the convergence of the secant method?

The choice of initial guesses can greatly impact the convergence of the secant method. If the initial guesses are not close enough to the actual solution, the algorithm may not converge at all. It is important to choose initial guesses that are close to the true solution for the secant method to be effective.

5. Can the secant method be used to solve any type of equation in MATLAB?

The secant method can be used to solve any type of equation that can be written in the form f(x) = 0. However, it may not always converge or may take a large number of iterations to converge for certain types of equations. In these cases, other root-finding methods may be more effective.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Replies
4
Views
69
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Advanced Physics Homework Help
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
7K
Back
Top