How to solve for a variable multiple times using a for loop.

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

Star Strider
Star Strider am 21 Sep. 2015
Bearbeitet: Star Strider am 21 Sep. 2015
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 Kommentare
Cyrus Azizzadeh
Cyrus Azizzadeh am 2 Jan. 2022
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
Star Strider am 2 Jan. 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by