Help with a Script using nested for and fzero function

4 views (last 30 days)
Hi guys,
I need to implement a script that will find the zeros of a function and after that plot the value of alpha(x axis) vs beta(y axis).
the function is given by:
(cos(phi)*sin(beta-acos(cos_phi))-a)+(a-cos(phi)*sin(alpha-acos(cos_phi))*exp^(-(beta-alpha)/tang(acos(cos(phi))))
The program that i wrote till now is:
for a=0:0.2:1
for cos_phi=0:0.2:1
for alpha=0:0.1:pi
beta_calc= @(beta,alpha,cos_phi,a) (cos(phi)*sin(beta-acos(cos_phi))-a)+(a-cos(phi)*sin(alpha-acos(cos_phi))*exp^(-(beta-alpha)/tang(acos(cos(phi)))))
teste=fzero(@(beta) beta_calc(beta), [pi/2 2*pi])
plot(alpha,beta)
end
end
end
But obviously is not running and im stuccoed :(
Can someone help me?
I am new on matlab so this is a kind of high level for me...
Thanks in advance!

Accepted Answer

Walter Roberson
Walter Roberson on 6 Oct 2017
Code attached.
I had to assume that tang() means tan()
The search range for fzero has to be constrained so that beta >= alpha: otherwise the sign of the term in the exp() can change, leading to the problem that the endpoints of the fzero search are not of opposite sign.
You will notice that all of the graphs are exactly the same. If you work through the formula you posted, you will find that it is of the form
(something - a) + (a - something_else)
which is equivalent to (something - a + a - something_else), and so the a's cancel out.
  16 Comments
Walter Roberson
Walter Roberson on 8 Oct 2017
Okay, we are getting much closer. See attached.
I replaced the direct calls to fzero with calls to a routine intended to search for multiple zeros. The routine divides the given range up into the given number of subdivisions, and looks for all transitions in sign under the assumption that the user specified enough subdivisions that all of the transitions will be present. It finds extracts the transition intervals and does fzero() on each of the intervals to get the more exact location. The routine returns all of the detected transitions.
With the aid of this routine, I was able to find that each combination has between one and three zeros. Some of the zeros are not crossings, being points where the curve just touches zero from below and then goes negative again; I found that I was able to detect those by adding in the special points that are simple fractions of pi (e.g., 1/8 * pi). Or at least I was able to detect them well enough that I was able to find at least one zero for each combination.
I decided to plot all three zeros. In the graph that is produced, the largest of all of the zeros is associated with the ^ marker, including if that is the only zero. In cases where there are more than one zero, the smallest of the zeros is associated with the v marker. In cases where there are three zeros, the middle zero is associated with the + marker.
You will notice that a number of points are drawn below 90, including most of the lower and middle markers. In the plot you see from running the code, none of the upper markers are below 90. That is, however, only because I tweaked the code. In the situation where cos(phi) is sufficiently close to 1, the markers are below 90; in particular for cos(phi) = 1 exactly, the markers are at asind(a), which is never more than 90.
For alpha above roughly 130, there are lower beta greater than 90 for most a values. For a = 0.8 there are also middle beta greater than 90 -- so if the cut-off for drawing is to be [90 360] then you will still get some values plotted.
You might notice that the solid lines (upper beta) appear to vanish at sufficiently high alpha. Those lines are not absent: they are merely all the same positions, so the line drawn last is the one whose color is visible.
Jucimar Carpe
Jucimar Carpe on 8 Oct 2017
ok, i will try to run into.
Thank you very for your kindly support!!!!

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!