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 个)
jgg
2016-2-29
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!