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

2 views (last 30 days)
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

Accepted Answer

Thorsten
Thorsten on 2 Dec 2014
Edited: Thorsten on 2 Dec 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);

More Answers (1)

Pietro
Pietro on 2 Dec 2014
Thanks! That was very helpful!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!