my code is not running

1 回表示 (過去 30 日間)
edward desmond muyomba
edward desmond muyomba 2020 年 4 月 21 日
編集済み: Rik 2020 年 4 月 21 日
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 件のコメント
edward desmond muyomba
edward desmond muyomba 2020 年 4 月 21 日
the code is not runing
Image Analyst
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.

サインインしてコメントする。

採用された回答

Deepak Gupta
Deepak Gupta 2020 年 4 月 21 日
編集済み: Rik 2020 年 4 月 21 日
Hello Edward,
Looking at your code, seems like you are trying to solve an euqation using Newton's Mathod. I see many errors in your code. So i have written a new code to solve the equation, you are trying to solve. Have added comments along the lines for you to understand.
syms f(x) x; %Symbolic variables needs to be defined before using them
f(x) = (x*exp(x)+0.3*x-0.5)/(x-5); % Equation you want to solve
g = diff(f); %To find the derivative of function
epsilon=1e-6;%Max allowed deviation
x = zeros();
x(1)= -1 ;%In Matlab index starts from 1, so this is the initial value of x.
%No need to create another variable for it.
i =1;
while(abs(f(x(i)))>epsilon) %Loop should be stopped when tolerance is reached
y(i) = double(f(x(i))); %If you need function values too, otherwise, not needed
x(i+1) = x(i) - f(x(i))/g(x(i));% Calculating values for x iteratively
i = i+1;
end
Thanks,
Deepak
  1 件のコメント
edward desmond muyomba
edward desmond muyomba 2020 年 4 月 21 日
this has worked out thanks pham

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by