my code is not running
이전 댓글 표시
y=(x.*exp(x)+0.3*x-0.5) ./(x-5);
y_prime=((x-5) .*(x.*exp(x)+exp(x)+0.3)-x.*exp(x)+0.3*x-0.5)) ./(x-5) .^2;
x_intial=-1;
epsilon=10^(-6);
newton(@(x)(x.*exp(x)+0.3*x-0.5) ./(x-5),@(x)((x-5).*exp(x)+exp(x)+0.3)-(x.*exp(x)+0.3*x-0.5)) ./(x-5).^2,-1,10(^6))
function [x]= newton(y,y_prime,x_initial,epsilon)
x(-1) =x_initial;
i = 1;
while abs(y(x(i))>epsilon)
x(i+1) = x(i) - y(x(i))/y_prime(x(i));
i = i+1;
end
fprintf('this answer is x=%.4f',x(i))
end
댓글 수: 5
Rik
2020년 4월 21일
Why are you trying x(-1)=x_intitial? Your code looks like x could be a scalar instead.
edward desmond muyomba
2020년 4월 21일
Tommy
2020년 4월 21일
In addition to the above comment...
This line:
y_prime=((x-5) .*(x.*exp(x)+exp(x)+0.3)-x.*exp(x)+0.3*x-0.5)) ./(x-5) .^2;
has a syntax error, one too many parentheses. Maybe you mean this instead?
y_prime=((x-5) .*(x.*exp(x)+exp(x)+0.3)-x.*exp(x)+0.3*x-0.5) ./(x-5) .^2;
This line:
newton(@(x)(x.*exp(x)+0.3*x-0.5) ./(x-5),@(x)((x-5).*exp(x)+exp(x)+0.3)-(x.*exp(x)+0.3*x-0.5)) ./(x-5).^2,-1,10(^6))
contains a few syntax errors. You are calling your function, newton, which takes four arguments, but it is a bit hard to tell what each of the four arguments should be. Maybe this is correct?
newton(@(x) (x.*exp(x)+0.3*x-0.5)./(x-5),... first arg
@(x) ((x-5).*exp(x)+exp(x)+0.3)-(x.*exp(x)+0.3*x-0.5)./(x-5).^2,... second arg
-1,... third arg
10^6) % fourth arg
This line:
abs(y(x(i))>epsilon)
MATLAB should be warning you that it is unexpected, because
y(x(i))>epsilon
will return a logical (1 or 0). Do you mean
abs(y(x(i)))>epsilon
instead?
edward desmond muyomba
2020년 4월 21일
Image Analyst
2020년 4월 21일
You need to click the green run triangle. That will run it. What else can I say given the incredibly detailed explanation you gave? ?♂️
Attach the entire script -- everything including the part where you define x - with the paper clip icon.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!