hi, i'm planning to reach the root of this function:
exp(-x) - sinx = 0;
here is my code:
------------------------------
x0 = input('please enter x0 ::: ');
eps = input('please enter steps ::: ');
% eps is the step
iterator = 0;
for i=0:eps:x0
x=x+exp(-x) - sin(x);
iterator = iteratio + 1;
end
disp('root ::: '); disp(x);
disp('iterate count ::: '); disp(iterator);
------------------------------
well i can't make it work. can you help me with that?

답변 (2개)

Mischa Kim
Mischa Kim 2014년 1월 10일

0 개 추천

Try the fzero function. Unless, of course, you would like to do it on your own.
reza
reza 2014년 1월 10일

0 개 추천

Now it's working,
x0 = input('please enter x0 ::: '); eps = input('please enter steps ::: '); iterator = 0; x=0; iterator = 0; for i=0:eps:x0 x=x+exp(-x) - sin(x); iterator = iterator + 1; end disp('root ::: '); disp(x); disp('iterate count ::: '); disp(iterator);
thanks to all.

댓글 수: 6

Mischa Kim
Mischa Kim 2014년 1월 11일
Just want to make sure you are getting the results you are looking for. What root-finding method are you using? For those kind of problems I'd recommend Newton-Raphson (if you prefer coding yourself). Also note that this function has an infinte number of roots. Here are two of them:
r1 = fzero(@(x) exp(-x) - sin(x), 0)
r1 =
0.5885
r2 = fzero(@(x) exp(-x) - sin(x), 2)
r2 =
3.0964
Youssef  Khmou
Youssef Khmou 2014년 1월 11일
that is good remark, so how, for example, we find the period of the solutions using only fzero as you illustrated above?
Mischa Kim
Mischa Kim 2014년 1월 11일
Not sure I understand what you are asking. If it's about finding more than just a handful of roots, you could use a for -loop. For positive x-values the function (and therefore the roots) is dominated by the sine-term, which allows you to get pretty accurate starting values for the searches.
Youssef  Khmou
Youssef Khmou 2014년 1월 11일
편집: Youssef Khmou 2014년 1월 11일
yes, here is the result :
ct=1;
for n=0:100
F(ct)=fzero(@(x) exp(-x)-sin(x),n);
ct=ct+1;
end
stem(F)
can we conclude that there re 4 solutions?
Mischa Kim
Mischa Kim 2014년 1월 11일
By solutions you mean roots? Here is the function plot:
Youssef  Khmou
Youssef Khmou 2014년 1월 11일
편집: Youssef Khmou 2014년 1월 11일
alright, infinite number of solutions with period of ~3.1
thanks

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

질문:

2014년 1월 10일

편집:

2014년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by