How can I find the maximum of sin(x) through matlab on an interval?
Using just if statements, I need to find the maximum point of sin(x) on a closed interval (a,b). I know to check for if either sin(a) or sin(b) is 1. I also know that if b-a is greater than 2 pi, the maximum would be 1.
If none of these statements are true, I can't figure out how to check for if the equation has a value of 1 between those points through just if statements.
First check for the (b-a) > 2pi.
Then you could use the fact that the derivative of sine is cosine. Hence if cos(a) is positive and cos(b) is negative you have peak in between, if cos(a) is negative & cos(b) positive its a valley. If cos(a) and cos(b) are same sign, then check if (b-a) > pi to figure if you have a peak. Last it's sin(a) vs sin(b).
Makes sense? :)
Having a, calculate the next point with sin(x)==1. Then compare x with b to check if 1 is within your interval.
Steps to do so:
- subtract shift
- divide by 2 pi
- round up, we want a maximum right to a
- reverse 2 and 1
candidate=(ceil((a-pi/2)/pi/2)*pi*2)+pi/2 if candidate<b candidate is maximum