Assign variables from a solve solution

18 Ansichten (letzte 30 Tage)
Rihards
Rihards am 27 Okt. 2022
Kommentiert: Chunru am 27 Okt. 2022
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

Akzeptierte Antwort

Chunru
Chunru am 27 Okt. 2022
Bearbeitet: Chunru am 27 Okt. 2022
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 Kommentare
Rihards
Rihards am 27 Okt. 2022
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 am 27 Okt. 2022
See the update above. All your results are stored in struct array now.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by