Using fsolve to generate an array of answers

2 views (last 30 days)
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 Comment
Matt Fig
Matt Fig on 3 Nov 2012
There is a little button that looks like this: {} Code
Please use it....

Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 3 Nov 2012
Edited: Matt Fig on 3 Nov 2012
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 Comments
Matt Fig
Matt Fig on 3 Nov 2012
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)
Mike
Mike on 3 Nov 2012
That worked but there is still something wrong with my code and probably on my end with the formula...thank you for all your help!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!