display function does not show the exact value

4 views (last 30 days)
I am new to MATLAB. I wrote code like this;
syms n
f(n) = 40*n.^1.5-875*n+35000;
fdiff(n) = diff(f(n));
x0 = 100;
xprev = 0;
while (abs(x0 - xprev)/x0)*100 >= 1
xprev = x0;
x0 = x0 - f(x0)/fdiff(x0);
end
display(x0);
When I tried to display the value of x0, it shows;
600/11 - (40*(600/11 - ((24000*11^(1/2)*600^(1/2))/121 - 140000/11)/((60*11^(1/2)*600^(1/2))/11 - 875))^(3/2) + (875*((24000*11^(1/2)*600^(1/2))/121 - 140000/11))/((60*11^(1/2)*600^(1/2))/11 - 875) - 140000/11)/(60*(600/11 - ((24000*11^(1/2)*600^(1/2))/121 - 140000/11)/((60*11^(1/2)*600^(1/2))/11 - 875))^(1/2) - 875) - ((24000*11^(1/2)*600^(1/2))/121 - 140000/11)/((60*11^(1/2)*600^(1/2))/11 - 875)
Instead of just;
62.6913
How can I make it to display only the final value?
  2 Comments
Andreas Sorgatz
Andreas Sorgatz on 21 Mar 2016
The answer is a representation of the "exact" value. However, you can get an approximation of this exact result using vpa(...) if you need high precision or using double(...) if hardware precision is fine.
Álvaro Sacristán Gonzalez
I fixed it with disp(vpa(x)), which gives you 32 digits of precision. Newton-Raphson function analysis here 2 :D

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Feb 2016
disp(double(x0))

More Answers (1)

John BG
John BG on 27 Feb 2016
Mustafa
If you are really after a numeric result, why don't start with numeric values?
x_range=10
x_step=.1
x=[0:x_step:x_range]
f=@(x) 40*x.^1.5-875*x+35000
y=f(x)
ydiff = diff(y)
x0 = 100
xprev = 0
while (abs(x0 - xprev)/x0)*100 >= 1
xprev = x0
x0 = x0 - f(x0)/ydiff(x0)
end
x0 =
-82.29
Play with x_range and x_step to center the curve wherever you need.
If you find this answer of any help solving this question, please click on the thumbs-up vote link,
thanks in advance
John

Community Treasure Hunt

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

Start Hunting!