Clear Filters
Clear Filters

"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."

2 views (last 30 days)
I am trying to use the following function but the error keeps occurring in Line 14:
function r = rxnrate(V,F)
global k Keq f P
Fa=F(1);
Fb=F(2);
Fc=F(3);
Fd=F(4);
Pa=(Fa./f)*P;
Pb=(Fb./f)*P;
Pc=(Fc./f)*P;
Pd=(Fd./f)*P;
Fa=Fb;
Fc=Fd;
f=Fa+Fb+Fc+Fd;
r(1)=-k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(2)=-k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(3)=k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(4)=k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r=r';
end
I am also trying to use this function for the following script:
%This script will plot the respective flowrate versus the volume of
%the reactor for the following reaction:
%CO + H2O <--> CO2 + H2 where CO = A, H2O = B, CO2 = C, H2 = D
clear all;
clc;
P=1; %System pressure in atm
R=8.314*10^-3; %Ideal gas constant in kJ/mol*K
k0=6.195*10^8; %Reference rate constant in mol/(atm^2*m^3*min)
f=33; %Inlet molar flowrate in kmol/min
Fao=(0.5*f); Fbo=(0.5*f); Fco=0; Fdo=0; %Inlet flow rates for all species
T1=450; %System temperature in K
T2=550; %System temperature in K
T3=650; %System temperature in K
k=k0*exp(-47.53/(R*T1)); %Rate constant with respect to temperature
Keq=exp((4577.8/T1)-4.33); %Equilibrium constant with respect to temperature
V=[0 0.25];
Fo=[Fao Fbo Fco Fdo];
[V,F]=ode45(@rxnrate,V,Fo);
plot(V,F);
Thank you for the help!

Accepted Answer

Star Strider
Star Strider on 26 May 2018
Please do not ever use global variables.
This works:
function r = rxnrate(V,F, k, Keq, f, P)
Fa=F(1);
Fb=F(2);
Fc=F(3);
Fd=F(4);
Pa=(Fa./f)*P;
Pb=(Fb./f)*P;
Pc=(Fc./f)*P;
Pd=(Fd./f)*P;
Fa=Fb;
Fc=Fd;
f=Fa+Fb+Fc+Fd;
r(1)=-k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(2)=-k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(3)=k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r(4)=k.*((Pa.*Pb)-(Pc.*Pd./Keq));
r=r';
end
P=1; %System pressure in atm
R=8.314E-3; %Ideal gas constant in kJ/mol*K
k0=6.195E8; %Reference rate constant in mol/(atm^2*m^3*min)
f=33; %Inlet molar flowrate in kmol/min
Fao=(0.5*f); Fbo=(0.5*f); Fco=0; Fdo=0; %Inlet flow rates for all species
T1=450; %System temperature in K
T2=550; %System temperature in K
T3=650; %System temperature in K
k=k0*exp(-47.53/(R*T1)); %Rate constant with respect to temperature
Keq=exp((4577.8/T1)-4.33); %Equilibrium constant with respect to temperature
V=[0 0.25];
Fo=[Fao Fbo Fco Fdo];
[V,F]=ode45(@(V,F)rxnrate(V,F, k, Keq, f, P),V,Fo);
plot(V,F);
The problem is that you did not declare the variables to be global in both your calling script and ‘rxnrate’. However, since you will never again use global variables, and will always pass necessary variables as additional parameters to your functions instead, there is no need to mention that further.
  2 Comments
Matthew Tom
Matthew Tom on 26 May 2018
Thank you for the help! I had a feeling it had something to do with the global variables. My professor suggested that I use global but I didn't know why. I really appreciate it and have a great day!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 26 May 2018
Edited: Walter Roberson on 26 May 2018
You use global variables in your script, but you have not initialized the global variables, so they are empty.
In your previous Question I told you,
"To run that code, you need to have first executed some code that used
global k Keq
and then assigned values to k and Keq."
You did not do that. You assigned to k and Keq, but you did not first declare them global.
When you declare a variable to be global, that statement only affects where the current workspace looks for a variable with that name. Declaring a variable global does not have any effect on any function that does not also specifically declare the variable to be global (exception: nested functions are able to see that the variable exists in the parenting function.)
If it were otherwise, if declaring a variable to be global truly affected use of every variable by that name in every function, then it would be impossible to write secure software.
You have just experienced a small part of how tricky it is to use global variables properly. We recommend against using global variables because they are so error prone. We recommend that you instead use http://www.mathworks.com/help/matlab/math/parameterizing-functions.html

Community Treasure Hunt

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

Start Hunting!