Using fsolve to generate an array of answers

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

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

Thank you for the help, I am still getting the fsolve stalled display when running the code. Also I now want to plot(X_ph,T2) but it says 'conversion from double to cell is not possible'. How can I plot my results?
Thanks again
And Im not sure what you mean about using i...? Im using it in the for loop...
By using the MATLAB function i as a variable, you are masking the function. Check it out:
clear all
4+5*i % i is a function that returns sqrt(-1)
for i = 1:3,i;end % Now the function is masked. :(
4+5*i % Oops. We needed to use the function...
% Perhaps use k or n instead (j is also a function).
Anyhow, to plot you can extract the X_ph. I didn't run the code, so are the X_ph all the same size?
yes they look to be the same size in the workspace
Fortunatly I am not using complex math here so i is okay, but now I see your point...
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)
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)

Categories

Tags

Asked:

on 3 Nov 2012

Community Treasure Hunt

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

Start Hunting!