display function does not show the exact value

조회 수: 5 (최근 30일)
Mustafa Süve
Mustafa Süve 2016년 2월 27일
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
Andreas Sorgatz
Andreas Sorgatz 2016년 3월 21일
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
Álvaro Sacristán Gonzalez 2021년 9월 26일
I fixed it with disp(vpa(x)), which gives you 32 digits of precision. Newton-Raphson function analysis here 2 :D

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 27일
disp(double(x0))

추가 답변 (1개)

John BG
John BG 2016년 2월 27일
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

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by