Operator '*' is not supported for operands of type 'function_handle'
3 views (last 30 days)
Show older comments
clear all;
clc;
r_value = 0:.01:.6;
r1 = r_value(:);
Z = [];
for i = 1 : length(r1)
r = r1(i);
i;
Z = [Z;fsolve(@(X)fun(X,r),1)];
end
function gss = fun(X,r)
S = X;
%%
gSq = 1.2;
%%
B = (100)^4;
Zp = 1.1;
Zn = 1.02;
Rp0 = 0.2/197.3;
Rn0 = 0.2/197.3;
%%
mS = 300;
mu = 2.16;
md = 4.67;
%%
pb = r*(197.3)^3;
t = 0;
pp = (1-(2*t))*pb/2;
pn = (1+(2*t))*pb/2;
%%
m1u = mu - gSq.*S;
m1d = md - gSq.*S;
%%
Rp = @(xup,xdp)fsolve(@(rp)f1(rp,xup,m1u,m1d,xdp,B,Zp),Rp0);
Rn = @(xun,xdn)fsolve(@(rn)f2(rn,xun,m1u,m1d,xdn,B,Zn),Rn0);
%%
Aup = @(xup)((sqrt(xup.^2 + (m1u*Rp)^2)) - (m1u*Rp)) ./ ((sqrt(xup.^2 + (m1u*Rp)^2)) + (m1u*Rp));
F1p = @(xup)((xup.*sin(xup)).^2 - Aup(xup).*((sin(xup)).^2 + (xup.*cos(xup)).^2 - (xup.*sin(2.*xup))));
xup = fsolve(@(xup,Rp) F1p(xup), 1);
%%
Aun = @(xun)((sqrt(xun.^2 + (m1u*Rn)^2)) - (m1u*Rn)) ./ ((sqrt(xun.^2 + (m1u*Rn)^2)) + (m1u*Rn));
F1n = @(xun)((xun.*sin(xun)).^2 - Aun(xun).*((sin(xun)).^2 + (xun.*cos(xun)).^2 - (xun.*sin(2.*xun))));
xun = fzero(@(xun) F1n(xun), 1);
%%
Adp = @(xdp)((sqrt(xdp.^2 + (m1d*Rp)^2)) - (m1d*Rp)) ./ ((sqrt(xdp.^2 + (m1d*Rp)^2)) + (m1d*Rp));
F2p = @(xdp)((xdp.*sin(xdp)).^2 - Adp(xdp).*((sin(xdp)).^2 + (xdp.*cos(xdp)).^2 - (xdp.*sin(2.*xdp))));
xdp = fzero(@(xdp) F2p(xdp), 1);
%%
Adn = @(xdn)((sqrt(xdn.^2 + (m1d*Rn)^2)) - (m1d*Rn)) ./ ((sqrt(xdn.^2 + (m1d*Rn)^2)) + (m1d*Rn));
F2n = @(xdn)((xdn.*sin(xdn)).^2 - Adn(xdn).*((sin(xdn)).^2 + (xdn.*cos(xdn)).^2 - (xdn.*sin(2.*xdn))));
xdn = fzero(@(xdn) F2n(xdn), 1);
%%
Oup = sqrt(xup^2 + (m1u*Rp)^2);
Oun = sqrt(xun^2 + (m1u*Rn)^2);
Odp = sqrt(xdp^2 + (m1d*Rp)^2);
Odn = sqrt(xdn^2 + (m1d*Rn)^2);
%%
M1p = (2*Oup + Odp - Zp)/Rp + 4*pi*Rp^3*B/3;
M1n = (2*Odn + Oun - Zn)/Rn + 4*pi*Rn^3*B/3;
%%
Sup = ((Oup/2) + (m1u*Rp*(Oup-1)))/((Oup*(Oup-1)) + (m1u*Rp/2));
Sun = ((Oun/2) + (m1u*Rn*(Oun-1)))/((Oun*(Oun-1)) + (m1u*Rn/2));
Sdp = ((Odp/2) + (m1d*Rp*(Odp-1)))/((Odp*(Odp-1)) + (m1d*Rp/2));
Sdn = ((Odn/2) + (m1d*Rn*(Odn-1)))/((Odn*(Odn-1)) + (m1d*Rn/2));
%%
DMpS = gSq*(2*Sup+Sdp);
DMnS = gSq*(Sun+2*Sdn);
%%
kp = (3*pp*pi^2)^(1/3);
kn = (3*pn*pi^2)^(1/3);
k1p = sqrt(kp^2 + M1p^2);
k1n = sqrt(kn^2 + M1n^2);
%%
Ip = M1p*((M1p^2*log(M1p^2))/4 - (M1p^2*log(kp + k1p))/2 + (kp*k1p)/2);
In = M1n*((M1n^2*log(M1n^2))/4 - (M1n^2*log(kn + k1n))/2 + (kn*k1n)/2);
F = ((1/pi^2) * (1/mS^2) * ((DMpS * Ip) + (DMnS * In))) - S;
gss = F ;
end
%%
function r = f1(rp,xup,m1u,m1d,xdp,B,Zp)
r = ((2*xup^2./sqrt(xup^2 + (m1u.*rp).^2)) + (xdp^2./sqrt(xdp^2 + (m1d.*rp).^2)) - Zp - (4*pi*B.*rp.^4));
end
function r = f2(rn,xun,m1u,m1d,xdn,B,Zn)
r = ((xun^2./sqrt(xun^2 + (m1u.*rn).^2)) + (2*xdn^2./sqrt(xdn^2 + (m1d.*rn).^2)) - Zn - (4*pi*B.*rn.^4));
end
In the above code i want to calculate S. within which we need to calculate xup,xdp,xun,xdn. where the Rp and Rn are again xup,xdp,xun,xdn dependent. I did it in this way but can not able to get result. Could you please help me out in this problem? I'm getting the problem like : "Operator '*' is not supported for operands of type 'function_handle'."
0 Comments
Accepted Answer
Star Strider
on 27 Aug 2023
The first time this occurs is here (line 35):
Aup = @(xup)((sqrt(xup.^2 + (m1u*Rp)^2)) - (m1u*Rp)) ./ ((sqrt(xup.^2 + (m1u*Rp)^2)) + (m1u*Rp));
where ‘Rp’ has previously been defined as (line 32):
Rp = @(xup,xdp)fsolve(@(rp)f1(rp,xup,m1u,m1d,xdp,B,Zp),Rp0);
Function handles must be evaluated to use them in calculations, so in ‘Rp’ needs to have its arguments stated in order for that to work:
Aup = @(xup)((sqrt(xup.^2 + (m1u*Rp(arg1,arg2))^2)) - (m1u*Rp(arg1,arg2))) ./ ((sqrt(xup.^2 + (m1u*Rp(arg1,arg2))^2)) + (m1u*Rp(arg1,arg2)));
whatever ‘arg1’ and ‘arg2’ are supposed to be.
You will need to go through your code and supply all the arguments to all the function calls in order for your code to work.
.
2 Comments
Star Strider
on 28 Aug 2023
My pleasure!
I’m not certain what you mean by ‘where fsolve will give me a function value and then it continues’ . You can call fsolve in your code and then use those values in subsequently.
Also, since you already know the value of ‘r1’, this version of the first loop will be more efficient —
Z = zeros(numel(r1),1);
for i = 1 : length(r1)
r = r1(i);
i;
Z(i) = fsolve(@(X)fun(X,r),1);
end
If ‘Z’ is a vector instead of a scalar in each iteration, make appropriate changes to the preallocation dimensions and then the ‘Z’ subscript dimensions in the loop.
.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!