How to solve for a variable multiple times using a for loop.
1 回表示 (過去 30 日間)
古いコメントを表示
I need to solve a nonlinear equation multiple times and plot the results. The equation is based on V, which is a constant, T, k & Kc, which both depend on T, and X. I'm trying to plot X as a function of T but can't seem to figure it out. When I attempt to use a for loop, I receive the error "Error using mupadmex Error in MuPAD command: Operands are invalid. [linalg::matlinsolve]".
x=1
for T = [300;1;400]
syms X
V = 3785.411784;
k = 1/2902.149*exp(15000*4.184/8.314*(1/300-1/T))
Kc = 3*exp(-25000*4.184/8.314*(1/300-1/T));
eqn = X/k/((1-X)^2-X^2/Kc)-V == 0;
Solution = double(vpasolve(eqn, X, [0 1]))
Solmat(x,1) = T;
Solmat(x,2) = Solution;
x=x+1;
end
0 件のコメント
採用された回答
Star Strider
2015 年 9 月 21 日
編集済み: Star Strider
2015 年 9 月 21 日
The problem is the semicolons (;) in the ‘T’ assignment at the start of the loop. Replace it with:
for T = [300:1:400]
and your code runs.
Also, you don’t need to define the ‘1’ step, since that is the default step value in colon (:) operator generated vectors.
2 件のコメント
Cyrus Azizzadeh
2022 年 1 月 2 日
How can I use "solve" command in "for loop"? My code:
Clc Clear all Syms t phi theta phi1 theta1 d=5; ss(1,1)=0 ss(1,2)=0
For t=5:5:350 A((t/5),1)= expression (t,phi,theta,phi1,theta1)
B((t/5),1)= expression (t,phi,theta,phi1,theta1)
A1((t/5),1)=subs(A((t/5),1),[phi,theta],[ss((t/5),1),ss((t/5),2])
B1((t/5),1)=subs(B((t/5),1),[phi,theta],[ss((t/5),1),ss((t/5),2]) s((t/5),1)= solve('A1((t/5),1)','B1((t/5),1)') s((t/5))=[s.phi1 s.theta1] ss((t/5))=eval(s((t/5))) end ss
Star Strider
2022 年 1 月 2 日
Post this as a new question. produce something meaningful for ‘expression’.
There are so many errors in that code I am not even going to attempt to correct them.
I will delete your Comment and this one in a few hours.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!