Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted
Comments
Ron Beyer 10-Jan-14 16:37pm    
"I can't make it work" isn't a description of a problem. Please tell us what problem you are having, we don't have a lot of Matlab experts here so you'll have to help narrow your problem down.
[no name] 10-Jan-14 17:13pm    
What is "sinx"...maybe I missed something, but I'm willing to learn.

And after this: If you have a minimum of the idea of the two functions in the equation you should find the answer.

If not please first try to learn about the pb and not simply ask about the solution!
m.r.m.40 10-Jan-14 17:58pm    
actually that was:
sin(x)
i guess i could solve it myself(the solution below),
thank you.
Matt T Heffron 10-Jan-14 17:42pm    
Assuming that x is correctly initialized to 0.0 before code given.
Remember that eps is used as the step size in the for loop, so it must be less than x0.
You seem to be attempting Newton's method, but it doesn't look correct.
I don't see the derivative of the exp(-x) - sin(x) represented anywhere.
m.r.m.40 10-Jan-14 17:58pm    
i guess i could solve it myself(the solution below),
thank you.

I strongly suggest to first get a "feeling" for the graph.
1) sin(x) is periodically in 2⋅π and has roots every n⋅π (∀ n ∈ Ν0)
2) sin(x) ≈ x (x much smaller than π/2)
3) e-x goes monotoinc to zero (∀ x ∈ R, x ≥ 0)
4) e-x has sample values at: e0 = 1, e ≈ 0.0432, e-2⋅π ≈ 0.0019, e-3⋅π ≈ 0.00008, e-4⋅π ≈ 0.0000034, ...

From this, you can postulate that the roots for e-x - sin(x) are

  • ≈ n⋅π (∀ n ∈ Ν) (since e-x for values x=π and above can be neglected)
  • ≈ 0.59 (for the root between 0 and π) (found by setting e-x ≈ x and estimating and roughly calculating some value between π/6 and π/4)


Knowing that, you can compare your numeric calculation with the aproximately estimated roots from above.

Your immediate problem was that x was not set to 0 initially.

My understanding is that you don't calculate the roots with your solution #1. It's calculating "something" but not the roots.

Describe first in your own words how you want to calculate the roots (e.g. by Regula Falsi, Newton, ...) and then only code that choosen approach.

Cheers
Andi
 
Share this answer
 
v3
Comments
Matt T Heffron 10-Jan-14 20:22pm    
+5
Andreas Gieriet 11-Jan-14 11:35am    
Thanks for your 5!
I have to fix the graph discussion - I made a mistake with the roots: *every* n⋅π is a root for sin(x) of course (not only every 2⋅n⋅π)
Cheers
Andi
Karthik_Mahalingam 11-Jan-14 11:46am    
5! for your effort ...
Andreas Gieriet 11-Jan-14 12:37pm    
Thanks for your 5!
Cheers
Andi
[no name] 11-Jan-14 13:08pm    
Great! +5Regards, Bruno

BTW Do you have an idea about that: http://www.codeproject.com/Questions/675324/m-s-bartlett-fitting-straight-line-always-three-gr
Now it's working,

VB
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.
 
Share this answer
 
Comments
Andreas Gieriet 10-Jan-14 18:36pm    
This is not calculating the roots, or am I missing something?
You should get roots at approximately 0.59, π, 2π, 3π, 4π, 5π, 6π, 7π. etc.
Cheers
Andi
[no name] 10-Jan-14 20:16pm    
my 1 :(
I recomend to check the solution #2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900