Assign variables from a solve solution

조회 수: 18 (최근 30일)
Rihards
Rihards 2022년 10월 27일
댓글: Chunru 2022년 10월 27일
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