# Thread: Secant method in MATLAB

1. ## Secant method in MATLAB

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. Can you help me repair my file?
This is my m-file:

Code:
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

2. Originally Posted by chronicals
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. Can you help me repair my file?
This is my m-file:

Code:
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
Have you tried running it? What happens? That is the least I expect when a question like this is asked.

It appears you have an infinite loop (which you should have mentioned), as you have an infinite loop you have a problem in the termination condition for the while loop.

Code:
clc
clear
n1=60;
n2=68;
tol=1e-3;
err=3;
iter=0;
fprintf('iteration n relative approximate error\n')

while err>=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=(abs((nnew-n2)/nnew))*100;
fprintf('%2d %f %f\n',iter,nnew,err)

if nnew>n1
n1=nnew;
else
n2=nnew;
end

end

nnew
iter

3. 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

4. Originally Posted by chronicals
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?
What do you want it to be? That is the relative change as a percentage.

CB

5. Originally Posted by CaptainBlack
What do you want it to be? That is the relative change as a percentage.

CB
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

6. Originally Posted by chronicals
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
Because you are using n2 as your old value when computing the relative percentage error rather than the last estimate used, n2 is not always the last estimate.

CB

7. Originally Posted by CaptainBlack
Because you are using n2 as your old value when computing the relative percentage error rather than the last estimate used, n2 is not always the last estimate.

CB
What should I use instead of n2, how I should change this error command?

8. Originally Posted by chronicals
What should I use instead of n2, how I should change this error command?
The following seems adequate:

Code:
clc
clear
n1=60;
n2=68;
tol=1e-3;
err=3;
iter=0;
fprintf('iteration n relative approximate error\n')
onew=68;

while err>=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=(abs((nnew-onew)/nnew))*100;
onew=nnew;
fprintf('%2d %f %f\n',iter,nnew,err)

if nnew>n1
n1=nnew;
else
n2=nnew;
end

end

nnew
iter
CB