Help with creating a Newton-Raphson algorithm function
조회 수: 6 (최근 30일)
이전 댓글 표시
As a part of a MATLAB course I've been asked to create a function that will solve a mathematical function by using Newton-Raphson algorithm.
So I've written the following function:
function s=newtRaph(Y,x1)
syms x
y=diff(Y,x);
Y=subs(Y,x,x1);y=subs(y,x,x1);
for i=1:4
xx=x1-(Y/y);
x1=xx;
end
s=x1;
And called it in the script like so:
syms x
y=x-cos(x);
newtRaph(y,3)
The problem is that i get the following ans instead of the solution:
ans =
(4*(cos(3) - 3))/(sin(3) + 1) + 3
Can anyone tell me what am I doing wrong?
댓글 수: 0
채택된 답변
madhan ravi
2018년 11월 17일
Can anyone tell me what am I doing wrong?
Since you created a function with a single output you have to also call it with an output .
as below:
Solution=newtRaph(y,3)
댓글 수: 5
madhan ravi
2018년 11월 17일
편집: madhan ravi
2018년 11월 17일
Anytime :) ,If it helped you make sure to accept the answer
추가 답변 (1개)
Greg Heath
2018년 11월 17일
편집: Greg Heath
2018년 11월 17일
Add the line
ans = ans
Hope this helps.
Thank you for formally accepting my answer
Greg
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!