display function does not show the exact value
조회 수: 5 (최근 30일)
이전 댓글 표시
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
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
2021년 9월 26일
I fixed it with disp(vpa(x)), which gives you 32 digits of precision. Newton-Raphson function analysis here 2 :D
채택된 답변
추가 답변 (1개)
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!