function handle producing different output

Hi,
I am trying to write a code to find the intersection of the two curve:
A = @(m) -2.*(1/1.895.*((1+(1.4-1).*m.^2/2).^(1.4/(1.4-1)))-1)./(1.4.*m.^2);
B = @(m) (0.38./sqrt(1-m.^2));
m_lower = 0;
m_upper = 1;
m_mid = (m_lower+m_upper)/2;
{while abs(A(m_mid))-(B(m_mid))) > 0
if (A(m_mid))<(B(m_mid))
m_lower = m_mid
else
m_upper = m_mid
end
m_mid = (m_lower+m_upper)/2
end
However, when I tried to plot the curves, (i.e. fplot(A,[0 1]);) it gives me an incorrect curve. but when I tried to solve individually A(1) etc. etc., it produce the correct answer. Similarly when I tried to loop the equation in the while loop, it just goes to infinity because it using the wrong curve.
Many thanks in advance!

1 Kommentar

www
www am 29 Feb. 2016
What do I have to do if I want to continue the loop is A(m_mid) ~= B(m_mid)? Is there another function or neater way?
Pardon me, I am very new to MATLAB!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Star Strider
Star Strider am 29 Feb. 2016

0 Stimmen

Your functions produce complex results, so the best you can hope for is to compare the absolute values of them:
A = @(m) -2.*(1/1.895.*((1+(1.4-1).*m.^2/2).^(1.4/(1.4-1)))-1)./(1.4.*m.^2);
B = @(m) (0.38./sqrt(1-m.^2));
AminusB = @(m) abs(A(m)) - abs(B(m));
IntSct(1) = fzero(AminusB, 0.5)
IntSct(2) = fzero(AminusB, 1.5)
x = linspace(0, 5, 100);
figure(1)
semilogy(x, abs(A(x)), x, abs(B(x)) )
hold on
plot(IntSct(1), abs(A(IntSct(1))), 'gp', 'MarkerSize',10)
plot(IntSct(2), abs(A(IntSct(2))), 'gp', 'MarkerSize',10)
hold off
grid
Producing:
IntSct =
754.2877e-003 1.3359e+000
I found the intersections by taking the differences between the absolute values of your functions, and then letting the fzero function find the zero crossings.

6 Kommentare

www
www am 29 Feb. 2016
is there a way to use while loop to execute a bisection method to produce a table of iterations?
This is how I would do it:
A = @(m) -2.*(1/1.895.*((1+(1.4-1).*m.^2/2).^(1.4/(1.4-1)))-1)./(1.4.*m.^2);
B = @(m) (0.38./sqrt(1-m.^2));
AminusB = @(m) abs(A(m)) - abs(B(m));
dw = 1E-4;
w = dw;
Val = AminusB(w);
while Val > 0
Val = AminusB(w);
w = w + dw;
end
fprintf('\nA = B at %.4f with value = %.4f\n', w, Val)
A = B at 0.7544 with value = -0.0001
www
www am 29 Feb. 2016
You have saved this poor student! T.T
Star Strider
Star Strider am 29 Feb. 2016
As always, my pleasure!
www
www am 29 Feb. 2016
Bearbeitet: Star Strider am 29 Feb. 2016
I was wondering how do I alter the code above to use the bisection method where I converge the intersection of A and B from a upper and lower limit and achieve a tabled results similar to : @8.54mins https://www.youtube.com/watch?v=ZJAPBTRI3Yk @8.54mins
My case would be to match A=B.
Star Strider
Star Strider am 29 Feb. 2016
The bisection method is a root-finding method, so I would use it with my derived ‘AminusB’ to find the root. That will be where A=B.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

jgg
jgg am 29 Feb. 2016

0 Stimmen

Not sure what the deal is, but this works:
A_vals = A(0.1:0.01:1);
vals = 0.1:0.01:1;
B_vals = B(0.1:0.01:1);
plot(vals,A_vals);
hold on
plot(vals,B_vals);
You can see it works here too:
fplot(B,[0.1,0.99])
hold on
fplot(A,[0.1,0.99])
I think the asymptotes are causing it to look funny; I'm not convinced it's wrong though.

Kategorien

Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

www
am 29 Feb. 2016

Kommentiert:

am 29 Feb. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by