Solve a numerical equation using for loop

5 ビュー (過去 30 日間)
MarshallSc
MarshallSc 2021 年 4 月 7 日
編集済み: DGM 2021 年 4 月 7 日
I'm trying to solve a numerical equation with with two variables x,y with for loop that are basically normalized of each other but I get the error below. I would really appreciate if someone could please help me. Here is the code and error message:
A=rand(10,10);
B=rand(10,10);
C=rand(10,10);
xAve=2;
t=0.1;
x = zeros(10,10);
y = zeros(10,10);
S = zeros(10,10);
syms x y
for i=1:10
for j=1:10
eqn=(x(i,j)*y(i,j))== (A(i,j)*B(i,j)*t^2)/(xAve*C(i,j));
S(i,j)=vpasolve(eqn,[x,y]);
end
end
Error:
Conversion to double from struct is not possible.
Thanks in advance.

採用された回答

DGM
DGM 2021 年 4 月 7 日
編集済み: DGM 2021 年 4 月 7 日
This might help:
psize=[10 10];
A=rand(psize);
B=rand(psize);
C=rand(psize);
xAve=2;
t=0.1;
S = struct('x',[],'y',[]);
syms x y
for i=1:psize(1)
for j=1:psize(2)
eqn=(x*y)== (A(i,j)*B(i,j)*t^2)/(xAve*C(i,j));
S(i,j)=vpasolve(eqn,[x,y]);
end
end
% look at one result
S(10,10).x
S(10,10).y
You were trying to put structs into a numeric array. That doesn't work. You were also simultaneously trying to use x and y as symbolic variables and numeric arrays. When used as numeric arrays, they'd make eqn1 invalid, since you're asserting that a nonzero constant is equal to zero. Not only is there nothing to solve, it's a false assertion.
Preallocate a struct to put your results in, and just use syms for x and y.
The last issue is that I don't know how you hope to solve the equation x*y=k, where k is a constant. You only have one equation, so the solution is a curve -- the locus of an infinite number of solutions. You might be able to graph these curves and obtain some information about them, but using a solver really doesn't tell you the whole story. For emphasis:
psize=[10 10];
A=rand(psize);
B=rand(psize);
C=rand(psize);
xAve=2;
t=0.1;
% plot the solution locus for the first element
x=linspace(0,1,100);
y=((A(1,1)*B(1,1)*t^2)/(xAve*C(1,1)))./x;
loglog(x,y); hold on; grid on
That's a line, not a point. There are infinitely many solutions to every one of these. vpasolve() will just give you one point on the line. It's lack of uniqueness likely makes the given solution meaningless in any particular application.
  2 件のコメント
MarshallSc
MarshallSc 2021 年 4 月 7 日
編集済み: MarshallSc 2021 年 4 月 7 日
That helped a lot. Thank you so much, really appreciate it.
Well, what I'm ultimately looking to solve is the equation below:
I which x and y corrospond to Li and Lk, A,B and C corrospond to Eijkl, Ri, deltaH. While I got you here, could you please take a look at it to see whether there is a method to solve this equation simultaneously? I would really appreciate your advice. The Li,Lj,Lk,Ll are interchangeable. The number of roots of this equation should be 16.
Thank you.
DGM
DGM 2021 年 4 月 7 日
Without breaking out pen and paper and trying to wrap my head around the whole thing, I'm not sure of any best way to solve it.
More generally, see if you can either simplify it to an equation of one variable, or suss out enough information to write two equations of two variables. If you can do the latter, you can just include it in your call to vpasolve().

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by