Assign variables from a solve solution

12 次查看(过去 30 天)
Hello!
I am having a slight problem where I am unable to assign the results of solve/vpa solve to a variable/variable array.
It has worked perfectly on an analogue problem in the previous parts of the same script, so I am unsure what I am missing here.
I am running the following for loop to find the solutions over the range of variable D2, and in this current setup the script runs well and gives me the solutions (correct), but as soon as I add "[Kr_x, alpha_x, k_x] = " in front of the vpasolve line, I'm getting the following error:
"Unable to find variables in equations"
Without the assignment to the three variables, I get the following answer:
ans =
struct with fields:
Kr_x: 0.99981170571100763658667832907699i
alpha_x: 97.041342847663022899757805866536
k_x: 0.029864918466521086498648092576702
So it seems like, it actually can find the variables?
syms k_x Kr_x alpha_x
c0=18.1499
alpha0=0.3491
g=9.81
omega=0.54
for i=1:length(D2)
c_x=c0*tanh(k_x*D2(i));
Snell_x=sin(alpha0)/c0==sin(alpha_x)/c_x;
Defraction_x=Kr_x==sqrt(cos(alpha0)/cos(alpha_x));
Linear_x = omega^2 == g*k_x*tanh(k_x*D2(i));
vpasolve([Snell_x, Defraction_x, Linear_x],[Kr_x, alpha_x, k_x])
end

采纳的回答

Chunru
Chunru 2022-10-27
编辑:Chunru 2022-10-27
syms k_x Kr_x alpha_x
c0=18.1499
c0 = 18.1499
alpha0=0.3491
alpha0 = 0.3491
g=9.81
g = 9.8100
omega=0.54
omega = 0.5400
D2 = [1 2];
for i=1:length(D2)
c_x=c0*tanh(k_x*D2(i));
Snell_x=sin(alpha0)/c0==sin(alpha_x)/c_x;
Defraction_x=Kr_x==sqrt(cos(alpha0)/cos(alpha_x));
Linear_x = omega^2 == g*k_x*tanh(k_x*D2(i));
% one ouput struct
sol(i) = vpasolve([Snell_x, Defraction_x, Linear_x],[Kr_x, alpha_x, k_x]);
end
sol(1)
ans = struct with fields:
Kr_x: 0.97020769905214767846260313371713 alpha_x: 100.5896791353776560831304307593 k_x: 0.17326753619233916799563671436781
sol(2)
ans = struct with fields:
Kr_x: 0.97103085879234422686236899857365i alpha_x: 97.306705145538144269628933596149 k_x: 0.12313256334502620836110299915066
  4 个评论
Rihards
Rihards 2022-10-27
Alright, in that case how do I assign the three different variables to their own arrays?
Like Kr_x(i), alpha_x(i)?
Chunru
Chunru 2022-10-27
See the update above. All your results are stored in struct array now.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by