Using fsolve to generate an array of answers
조회 수: 4 (최근 30일)
이전 댓글 표시
Hello,
I am trying to solve two simultaneous equations in a for loop and for each iteration I am changing temperature. I would like to generate an array of solutions such that I can plot verse temperature. Each iteration will generate two values associated with T.
A sample of the code is below:
T2=1809:1:2130; R=8.3144621; x3=[0.5,0.5]; %guess ao_s=18340; ao_l=20000; deltaH_Fe=15200; deltaH_Cr=20900; To_Fe=1809; To_Cr=2130;
for i=1:322
fun3=@(X) [(deltaH_Fe.*(To_Fe-T2(i))./To_Fe)./(R.*T2(i))+(ao_l./R.*... T2(i)).*(X(2).^2)-(ao_s./R.*T2(i)).*(X(1).^2)+log((1-X(2))./(1-X(1)))... ;(deltaH_Cr.*(To_Cr-T2(i))./To_Cr)./(R.*T2(i))+(ao_l./R.*T2(i)).*... ((1-X(2)).^2)-(ao_s./R.*T2(i)).*((1-X(1).^2)+log(X(2)./X(1)))];
X_ph=fsolve(fun3,x3);
end
I would like to generate X_ph either as 2x? matrix depending on how many temperature values or instead generate two separate matrix with values for X(1) in one and X(2) in another.I'm also getting an fsolve stalled error with this currently, not sure why this is happening either...
Any help would be greatly appreciated.
Thank you
댓글 수: 1
채택된 답변
Matt Fig
2012년 11월 3일
편집: Matt Fig
2012년 11월 3일
Use this:
X_ph{i}=fsolve(fun3,x3); % Note {} not ()
Now X_ph is a cell array.
help cell
Also, you might be sorry one day that you are masking the MATLAB function i by using this as a variable name.
댓글 수: 6
Matt Fig
2012년 11월 3일
So if you want to do:
plot(X_ph,T2)
And you are sure X_ph will return exactly two numbers for each call to fsolve (I didn't run the whole code till the end), the instead of using a cell, use:
X_ph(i,:)=fsolve(fun3,x3);
Now you can plot:
subplot(1,2,1)
plot(X_ph(:,1),T2)
subplot(1,2,2)
plot(X_ph(:,2),T2)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!