MATLAB Answers

How to know exact coordinates of an intersection of two functions using fplot and function handle?

6 views (last 30 days)
Meva
Meva on 4 Jan 2017
Edited: Meva on 4 Jan 2017
Hi,
This might be trivial to code. However I could not find the coordinates of intersection point.
I have two functions as below. I see the intersection of two lines. However, I need to know the exact coordinates of the intersection point such as when we do using sprintf( sprintf('theta2 = %22.18f',theta2) where theta2 is the intersection point (in x axis).
Another question: why fplot shows only certain limit of axes for RHS function. I know that RHS function has values out of the range in below plot. However, it only shows between -0.5 and 0 in x-axis.
b=1/2;
G=0.2;
M=1;
h2 = @(theta2) (1/2*(b*theta2 + sqrt(b^2*theta2^2-theta2/M)));
RHS = @(theta2) ( (1/2-b)- (h2(theta2)+(1-b)*theta2)^2/(theta2^2)*(h2(theta2)/(h2(theta2)+(1-b)*theta2) -...
h2(theta2)/(h2(theta2)-b*theta2)+ log((h2(theta2)+(1-b)*theta2)/(h2(theta2)-b*theta2)) ));
fplot(@(theta2) 4*G*M*theta2,'--or')
hold on
fplot(RHS,'--ob')
xlim([-10 10])
I also tried plotting with numerical values below:
numt2=-10:0.00001:10;
numh2 = (1/2*(b*numt2 + sqrt(b^2*numt2.^2-numt2./M)));
numRHS = ( (1/2-b)- (numh2+(1-b)*numt2).^2./(numt2.^2).*(numh2./(numh2+(1-b)*numt2) -...
numh2./(numh2-b*numt2)+ log((numh2+(1-b)*numt2)./(numh2-b*numt2)) ));
plot(numt2, numRHS)
hold on
plot(numt2, 4*G*M*numt2,'--or')
for i = 1 : length(numt2)
if (4*G*M*numt2(i)-numRHS(i)) < 10^(-11)
sprintf('t2 = %22.18f',numt2(i))
end
end
However, the if condition above (to see the intersection point) doe not work either!

  0 Comments

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 4 Jan 2017
Edited: John D'Errico on 4 Jan 2017
How would a plot possibly allow you to read off an intersection, with 16 significant digits of accuracy? 2 significant digits is as much as you could hope for.
Then you tried to find the intersection by plotting a LOT of points. But did you plot the curves, and seriously look at what that plot showed?????? Did you think about what you saw?
h2
h2 =
function_handle with value:
@(theta2)(1/2*(b*theta2+sqrt(b^2*theta2.^2-theta2/M)))
ezplot(h2,[-10,10])
grid on
Be careful though. For theta between 0 and 4, your function is not real valued!
h2(1)
ans =
0.25 + 0.43301i
h2(2)
ans =
0.5 + 0.5i
So, that ezplot connected the points at 0 and 4 is not relevant.
Next, do the same for your RHS function.
ezplot(RHS)
Warning: Function failed to evaluate on array inputs; vectorizing the function may speed up its evaluation and avoid the need to loop over array elements.
> In ezplot>ezplot1 (line 488)
In ezplot (line 144)
grid on
See that this function only returns real results for theta in the OPEN interval (-0.5,0). Outside of that interval, it returns either complex results, or NaN.
At the endpoints of the open interval (-0.5,0), RHS generates NaN as a result. It is undefined.
RHS(0)
ans =
NaN
RHS(-.5)
ans =
NaN
As well, everywhere in the interval where RHS DOES return a real result, it is ALWAYS negative. Those two curves NEVER intersect. PERIOD.
Think about what you are doing. Apply common sense. Failure to do so, just trying to throw something at a computer, will generally result in garbage.
If you actually had a problem worth solving, i.e., one that had an actual solution, then I would use fzero to find the intersection. Solve the problem
h2(theta)-RHS(theta)=0.
This is irrelevant, since there is NO solution.

  1 Comment

Meva
Meva on 4 Jan 2017
Thanks, but I had already plotted the two function.
You took h2() as one of my functions.
However, I had RHS and 4*G*M*theta2 to be plotted ( h2() is just a function inside of RHS(), it was not meant to be plotted ) as in the code in the question.
There is one intersection point.I just wanted to know what that is . Thanks for the response.I will use fzero.

Sign in to comment.

Sign in to answer this question.


Translated by