In an assignment A(I) = B, the number of elements in B and I must be the same.

Hello everyone! I'm having some issues trying to solve my code.
%%%---MAIN---%%%
clear all
close all
clc
global P ps
Ti=273.15;
Tf=1273.15;
T=linspace(Ti,Tf,10);
P=0.1*1e3;
k=[0 1 2 3 4 5];
ak=[6.1138e-1 4.4053e-2 1.4594e-3 2.6092e-6 2.8332e-7 2.7316e-9];
psat=ak(1)*(T-Ti).^k(1)+ak(2)*(T-Ti).^k(2)+ak(3)*(T-Ti).^k(3)...
+ak(4)*(T-Ti).^k(4)+ak(5)*(T-Ti).^k(5)+ak(6)*(T-Ti).^k(6); %[kPa]
options=optimset('TolFun',1e-5, 'MaxFunEvals',1e5, 'MaxIter',1e3,'Display','off');
for i=1:length(psat)
ps=psat(i);
mol_frac(i)=fsolve(@molar_fraction,[0.5 0.5],options);
end
%%%---FUNCTION---%%%
function f=molar_fraction (x)
global P ps
f=[x(1)-ps./P.*x(2); x(1)+x(2)-1];
end
this is the error I get from matlab:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Curve_sat_p (line 27) mol_frac(i)=fsolve(@molar_fraction,[0.5 0.5],options);
If I put a value of ps inside the function works but I don't know why it doesn't work with the for cycle.
Thanks to whoever is going to answer!
PM

 Réponse acceptée

Thorsten
Thorsten le 2 Déc 2014
Modifié(e) : Thorsten le 2 Déc 2014
Your function f returns a 2x1 column vector that cannot be assigned to a scalar mol_frac(i); instead, use
mol_frac(:,i)= fsolve(@molar_fraction,[0.5 0.5],options);

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by