C++ program help

The Saint

Gawd
Joined
Mar 18, 2003
Messages
1,000
Guys...

I need a little help here:
I need to write a program in C++ that solves the equation 5sin(2x)=e^(x/2) for 0<=x<=2 and displays the roots on the screen.

Now: I'm using a for statement(i=0; i<=2;i++) with an if statement if(y==0) nested inside.

Problem is that the program displays only one root....but not the other! How can I make it displays both roots? Use two different for statements?

Thank you.
 
It would probably be best to show the code you've got so far.
 
Guys...

I need a little help here:
I need to write a program in C++ that solves the equation 5sin(2x)=e^(x/2) for 0<=x<=2 and displays the roots on the screen.

Now: I'm using a for statement(i=0; i<=2;i++) with an if statement if(y==0) nested inside.

Problem is that the program displays only one root....but not the other! How can I make it displays both roots? Use two different for statements?

Thank you.

store the answers into global and print out the global when your done calculating
 
The loop you show considers only x = {0,1,2}. None of these are solutions to that equation; so how can you possibly be showing one of them?
 
a) you should almost never use strict equality with floating point values - instead, define something "close enough" (10^-6 or something) to the value you want and check to see if you're within a window around the desired value.

b) there's -far- better algorithms than a simple linear search for finding 0s of a function - a numerical methods/analysis text will be full of them. Assuming there's a pedagogical reason for using this approach (ie - learning about iteration), you should be using -far- smaller steps. Throwing integers into the mix might be causing you some problems as well.


store the answers into global
uhh... global? this sounds like bad advice. very bad.
 
I don't think amoeba is worried about anyone dying. I think, instead, he's worried about a student picking up bad habits.
 
Thank you so much guys.....

well you have t understand that I began programming about 2 weeks ago: I'm taking a summer class. I know nothing about globals and/or anything else that is too advanced.

We only covered the for statement, if, do ....

So basically I Understand that I cannot be using integers with a stepof 1 to check if the equation gives me a zero, correct?
Instead of i++ I should use something like i += 0.1?

Thank you so much
 
So basically I Understand that I cannot be using integers with a stepof 1 to check if the equation gives me a zero, correct?
Instead of i++ I should use something like i += 0.1?

Thank you so much

Unless you know that the roots are all integers, you'll need to consider non-integer values. Should you use 0.1, or 0.01, or 0.000001?

How many roots do you expect to find?

By the way, how would one solve this with a pencil and paper? Taylor series expansions? How?
 
Well if I use something like 10^-10 it would be more precise...

I would expect to find two roots for this particular equation.....and yes probably I would use the Taylor expansion since sin(X) and exp are relatively easy...
 
you could also try a variable increment value. since both expressions are continuous in the interval, if you see the values converging upon one another you can update with smaller increments as the expressions near one another, and large values as they diverge. that would allow you to search through a broader range faster, while still allowing for a fairly close answer, without deviating from the basic tools you've been given to figure this out. you could apply similar logic toward evaluating the taylor expansions as well.

remember that regardless of how small you make your increments, you'll need to test within an uncertainty of +/- some value.
 
I would use an error minimizing algorithm myself. If neither incrementing your search value up or down produces lower error, then you would decrease your increment value and search again. Once you have found a search value with lower error, you repeat the process on the new value. You are done when your error is below a certain threshold. AFAIK this is how languages like matlab work to do this (ie/ fminsearch and fzero functions) .

One issue with this technique is that the search can converge on local error minima which are not a solution (in general; this particular function may not have local minima). You'll have to account for this by detecting convergence on a high error value.

Then you can iterate through the solution space with a fairly coarse step as you are doing now. In fact you could cheat and run the algorithm on values close to the known solutions. Often this is what I will do to find the zeros: I will plot the LHS and RHS on each other, check for the points of intersection visually, and then use those rough values in a numerical solution like the one I have just discussed.
 
Since this is week 2 of an intro class - I think you're on the 'right' track with the for loop, as long as you figure out how to do this with floats.

If you're going to use something clever and more complex after asking us here on the forums, it's probably best to do -both- the complex solution and the simplistic one, or else the instructor might think you're having somebody else do your homework.
 
Since you know the function, you can find its derivative, and use Newton's Method... If you don't want to do that, try the secant method. They converge very quickly.

EDIT: So glad I can give input from my Numerical Analysis class... I never thought this day would come.
 
Back
Top