How do I multiply two functions handles?
2 次查看(过去 30 天)
显示 更早的评论
%%---------constants:
Gma_32 = 100
Gma = 210e-15
gamma_u = 500
gamma_c = 50
a = Gma_32/sqrt(2*gamma_u*Gma)
n = 4e10
c1 = -4
c2 = 2.05
c3 = 1
K = 10
%--------Function
%
% X = @(k) -4*n - a^2*n*sqrt(n) + 4*c1*k.^4 + (2*a*sqrt(n)-16*K^2 + 4*a*c1*c3*sqrt(n) + 8*n*c1*c2)*k.^2
% Sq = @(k) sqrt((-X(k) + sqrt(X(k).^2 + Y^2 ))/2)
% f = @(k) -2*k.^2 - 2*n - a*sqrt(n) + Sq(k)
% f2 = @(k) -2*k.^2 - 2*n - a*sqrt(n) - Sq(k)
%------------------------------Fns:
b_i = @(k) -4*c1*k*K
b_r = @(k) 2*k.^2 +2*n + a*sqrt(n)
c_i = @(k) ( 4*n*c2* - 2*(2*n + a*sqrt(n))*c1 + 2*a*sqrt(n)*c3 )*K*k
c_r = @(k) -(1 + c1^2)*k.^4 - (2*n -4*(1 + c1^2)*K^2 + sqrt(n)*c3*c1 + 2*n*c1*c2 )*k.^2
D_r = @(k) b_r(k).^2 - b_i(k).^2 -4*c_r(k)
D_i = @(k) 2*b_i(k).*b_r(k) - 4*c_i
f = @(k) -b_i(k) + sqrt( -D_r(k) + sqrt(D_i(k)^2 + D_r(k)^2))/4
%-------Grid:
k = -200:0.1:200
% %-------Plot
%
plot(k,f(k))
%plot(k,f2(k))
theres an error on line with function f, how do i multiply two functoin handles. or suggest a bwtter way of plotting this function.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1032305/image.png)
1 个评论
Steven Lord
2022-6-14
FYI for the future, when you're asking for help with code that throws an error message or issues a warning message please include the full and exact text of the error or warning message in your original question. This information may help readers determine the cause of the error and how to correct it more quickly. Thanks.
采纳的回答
Star Strider
2022-6-14
In the ‘D_i’ function, ‘c_i’ was originally missing its argument, and that threw the error.
Correcting that, and vectorising the exponentiations produces —
%%---------constants:
Gma_32 = 100;
Gma = 210e-15;
gamma_u = 500;
gamma_c = 50;
a = Gma_32/sqrt(2*gamma_u*Gma);
n = 4e10;
c1 = -4;
c2 = 2.05;
c3 = 1;
K = 10;
%--------Function
%
% X = @(k) -4*n - a^2*n*sqrt(n) + 4*c1*k.^4 + (2*a*sqrt(n)-16*K^2 + 4*a*c1*c3*sqrt(n) + 8*n*c1*c2)*k.^2
% Sq = @(k) sqrt((-X(k) + sqrt(X(k).^2 + Y^2 ))/2)
% f = @(k) -2*k.^2 - 2*n - a*sqrt(n) + Sq(k)
% f2 = @(k) -2*k.^2 - 2*n - a*sqrt(n) - Sq(k)
%------------------------------Fns:
b_i = @(k) -4*c1*k*K;
b_r = @(k) 2*k.^2 +2*n + a*sqrt(n);
c_i = @(k) ( 4*n*c2* - 2*(2*n + a*sqrt(n))*c1 + 2*a*sqrt(n)*c3 )*K*k;
c_r = @(k) -(1 + c1^2)*k.^4 - (2*n -4*(1 + c1^2)*K^2 + sqrt(n)*c3*c1 + 2*n*c1*c2 )*k.^2;
D_r = @(k) b_r(k).^2 - b_i(k).^2 -4*c_r(k);
D_i = @(k) 2*b_i(k).*b_r(k) - 4*c_i(k);
f = @(k) -b_i(k) + sqrt( -D_r(k) + sqrt(D_i(k).^2 + D_r(k).^2))/4;
%-------Grid:
k = -200:0.1:200;
% %-------Plot
%
plot(k,f(k))
%plot(k,f2(k))
.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!