Assign variables from a solve solution
7 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Chunru
on 27 Oct 2022
Edited: Chunru
on 27 Oct 2022
syms k_x Kr_x alpha_x
c0=18.1499
alpha0=0.3491
g=9.81
omega=0.54
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)
sol(2)
4 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!