To find answer using matlabfunction

2 次查看(过去 30 天)
I am using matlabfunctions to try and solve for an equation for many possible variable combination, the script is executing but I am not sure it is what I am expecting.
Here is the program,
clear;clc;close all;
syms r1 r2 d ni
f = matlabFunction((ni-1).*((1./r1)-(1./r2)+(((ni-1).*d)./(ni.*r1.*r2))));
n = 100;
r1a = linspace(14.8e-3,15e-3,n);r2a = linspace(-14.8e-3,-15e-3,n);d = linspace(4e-3,4.1e-3,n);ni=linspace(1.51,1.518,n);
[R1,R2,D,N] = ndgrid(r1a,r2a,d,ni);
p = feval(f,R1,R2,D,N);
F = 1/p;
ind = find (F>0.014 & F<0.015);
Freq = F(ind);
r1req = R1(ind);I am
r2req = R2(ind);
dreq = D(ind);
nreq = N(ind);
Ar = [Freq,r1req,r2req,dreq,nreq];
What I am expecting here is the different combinations of n, r1, r2, and d thar gives f in range of 0.014 to 0.015
I am getting values till p right but while calculation F as 1/p I am getting values that are different to what I am anticipating.
If I am looking for F for the values of r1 = 15e-3;r2 = -15e-3;d = 4.049e-3;ni=1.5168; with above code I am getting 0.002 but with below code it is 0.015
clc;clear;close all;
r1 = 15e-3;r2 = -15e-3;d = 4.049e-3;ni=1.5168;
p = (ni-1).*((1./r1)-(1./r2)+(((ni-1).*d)./(ni.*r1.*r2)));
f = 1/p;
So how to correct the top code to get accurate f values.
Any kind of help is highly appreciable.
Thank you and regards.

采纳的回答

Dyuman Joshi
Dyuman Joshi 2024-4-4
You get the wrong value because the order of inputs is not correct.
By default, matlabFunction uses alphabetical order for the input arguments when converting symbolic expressions that contain only lowercase letters for the variable names. (Reference - https://in.mathworks.com/help/symbolic/sym.matlabfunction.html#d126e256587)
syms r1 r2 d ni
You can see the order of input to be provided from the definition of the function handle as well -
f = matlabFunction((ni-1).*((1./r1)-(1./r2)+(((ni-1).*d)./(ni.*r1.*r2))))
f = function_handle with value:
@(d,ni,r1,r2)(ni-1.0).*(1.0./r1-1.0./r2+(d.*(ni-1.0))./(ni.*r1.*r2))
n = 100;
r1 = 15e-3;
r2 = -15e-3;
d = 4.049e-3;
ni = 1.5168;
%Providing values in the correct order
p = feval(f,d,ni,r1,r2);
F = 1/p
F = 0.0152
As you can see, the value matches.

更多回答(0 个)

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by