How to use syms & solve in a function?
2 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a 3 equation 3 variable system which I can solve in cmd window using this code:
v1=10;
v2=5;
v3=4*I1;
r1=6;
r2=20;
r3=15;
r4=6;
r5=10;
rl=8;
eqn = [0==v1-(r1*I1)-(r2*I2),0==(r2*I2)-(r3*(I1-I2))+v2-(r4*I3)-(v3),0==(v3)+(r4*I3)-((r5+rl)*(I1-I2-I3))];
sol = solve(eqn);
sol.I1,sol.I2,sol.I3
This works fine and gives me the correct values for I1,I2,I3. I have more to do with set of equations. Specifically I need to solve for multiple values for RL so would like to make the above code a function that I can call from the cmd window.
I am struggling to work out how to structure the function. Could you please give me some hints on what would me the most efficient/eloquent?
0 件のコメント
採用された回答
Walter Roberson
2018 年 3 月 11 日
syms I1 I2 I3 rl
v1=10;
v2=5;
v3=4*I1;
r1=6;
r2=20;
r3=15;
r4=6;
r5=10;
eqn = [0==v1-(r1*I1)-(r2*I2),0==(r2*I2)-(r3*(I1-I2))+v2-(r4*I3)-(v3),0==(v3)+(r4*I3)-((r5+rl)*(I1-I2-I3))];
[I1, I2, I3] = solve(eqn, [I1, I2, I3]);
fun = matlabFunction([I1, I2, I3]);
You can now call fun() with a scalar or column vector argument of rl values. The corresponding I1, I2, I3 values will be returned across the columns.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!