the question is: Create a function which finds the root of an equation numerically by using the following recursive formula :

Xn+1=Xn-f(Xn)/g(Xn) for n-1,2,3,...
where g(Xn)=f(Xn+f(Xn))-f(Xn)/f(Xn). This iterative procedure must stop when the absolute difference between Xn and Xn+1 is less than a given tolerance epsilon. The funtion must accept as inputs a scalar function f, an initial number 'x' and a positive number epsilon to terminate the procedure. Hence use this numerical technique to find the root of the equation e^x-x^2=0.
HELP PLEASE :(
syms x
f=exp(x)-x^2;
y(1)=1;
if x(n)-x(n+1)<0.25;
for n=0:1:inf;
x(k+1)=yk-subs(f,x,yk)/subs(g(xn),x,yk);
where g(xn)=(f(xn+f(xn))-f(xn))/(f(xn))
end
end

4 Kommentare

(A) That is an iterative procedure that is described, not a recursive procedure;
(B) MATLAB does not have any "where" command;
(C) Your x is symbolic, so x(n) is symbolic, making it unlikely that the difference between the two will be numeric so that the difference can be tested in the "if" statement.
so how shall i arrange this to make it work ?
syms x g(xn) f(x)
f(x) = exp(x)-x^2;
g(xn) = (f(xn+f(xn))-f(xn))/(f(xn));
That takes care of the "where" part.
Note: this requires a fairly recent version of MATLAB, R2011b or later.
it is still not working however because of the 6th line :(

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Hi,
why using symblic math when doing it nurmerically anyway? You can do this with plain MATLAB right away:
f = @(x) exp(x) - x^2;
g = @(x) (f(x + f(x)) - f(x))/f(x);
x(1) = 0;
x(2) = x(1) - f(x(1))/g(x(1));
n = 2;
while abs(x(n) - x(n-1)) > eps
n = n + 1;
x(n) = x(n-1) - f(x(n-1))/g(x(n-1));
end

2 Kommentare

You can check the result with:
>> sprintf('%0.16f \n',f(x(end-1)))
ans =
-0.0000000000000002
>> sprintf('%0.16f \n',f(x(end)))
ans =
0.0000000000000000
Thanks so much !.. if you really want like a challenge, you should try figure out another of my questions by clicking the link: http://www.mathworks.com/matlabcentral/answers/75189-please-help-in-this-question-how-to-change-this-sentence-into-mathematical-terms
:)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by