What am i doing wrong?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chris Quinlan
el 29 de Dic. de 2020
Comentada: Chris Quinlan
el 31 de Dic. de 2020
I am currently trying to solve the following equations to gain a plot of temperature and molar flowrates of A, B and C all against volume with a cross sectional area assumption of 1 dm^3.
my code and the data are as follows but i keep getting parse and input errors, can anyone help please?
function F = Q1a(V,Y)
%series rxns
Fa = Y(1);
Fb = Y(2);
Fc = Y(3);
T = Y(4);
%defining k1 (dm^3 / mol min) & k2 (dm^6 / mol^2 min)
R = 8.314462; %j / mol k
k1 = 50 * exp((8000/R)*(1/315 - 1./500));
k2 = 400 * exp((8000/R)*(1/310 - 1./500));
K3 = k1/k2;
%defining heat capacities
%all cps J / mol k
Cpa = 20;
Cpb = 80;
Cpc = 100;
Cpi = Cpa;
Cpcool = 10;
%change in cp
DCp1 = Cpb - (2*Cpa);
DCp2 = Cpc - (2*Cpb) - Cpa;
%Temperatures k
T0 = 500;
Ta = 523;
%Enthalpy of rxns J / mol A
DH1 = -25000 + (DCp1 * (T - T0));
DH2 = 350000 + (DCp2 * (T - T0));
CT0 = 0.399; %mol / dm3
FT0 = 5; %mol / min
Ua = 150; %J / dm3 min k
FT = Fa + Fb + Fc;
Ca = CT0 * ((Fa/FT)*(T0./T));
Cb = CT0 * ((Fb/FT)*(T0./T));
Cc = CT0 * ((Fc/FT)*(T0./T));
%Defining rates
ra = (k1*(Ca^2)) - (K3*Cb) + (k2*Ca*(Cb^2));
rb = (k2*Ca*(Cb^2)) + (K*(Cb)) - (k1*(Ca^2));
rc = k2*Ca*(Cb^2);
r1 = (k1*Ca^2) - (K3*Cb);
r2 = k2*Ca*Cb^2;
%Defining Differential equations
dFadV = -ra;
dFbdV = -rb;
dFcdV = rc;
dTdV = ((Ua * (Ta - T) - ((r1*DH1)) - (r2*DH2))) / ((Cpa*Fa) + (Cpb*Fb) + (Cpc*Fc));
F = [dFadV; dFbdV; dFcdV; dTdV];
end
%% plotting
clear all;
Vspan = [0 10];
Y0 = [5 0 0 300];
[V, Y] = ode45(@Q1a, Vspan, Y0)
subplot(2,1,1)
plot(V,Y(:,1), VY(:,2), VY(:,3))
legend('Fa', 'Fb', 'Fc');
ylabel('MolarFlowrates, mol/min')
xlabel('Volume, dm3')
subplot(2,1,2)
plot(V,Y(:,4))
Legend('Temperature'/ 'K');
ylabel('Temperature' / 'K');
xlabel('Volume dm^3');

4 comentarios
Walter Roberson
el 31 de Dic. de 2020
rb = (k2*Ca*(Cb^2)) + (K*(Cb)) - (k1*(Ca^2));
uses K but K is not defined. Is k3 intended?
Once your temperature, Y(4), gets below 300, then the function oscillates rapidly, dropping to 0 quickly, and varying by +/- ten thousand-ish on trials with step size down around 1e-15. The system just cannot stabilize with negative temperature.
Respuesta aceptada
Walter Roberson
el 31 de Dic. de 2020
MATLB permits .m files with an indefinite number of functions stored in them, in which the first executable token of the .m file is function . The functions in such files can either all have an end matching their function statement, or else none of them can have that matching end . If you want to use nested functions with shared variables, then that matching end must be present (and must be present for all functions in the .m file.) The first function of the file is the only one that can be called directly from outside the .m file, and the name it is to be called by is the same as the name of the .m file, no matter what the name given on the first function line is... but using a function name different from the file name is not recommended. Because only the first function can be called directly, there is no way provided to execute any function other than the first from outside the file without the cooperation of the code. There is no restriction on the names of the functions inside the .m file in this case, as long as they do not duplicate each other and are valid MATLAB identifiers.
MATLAB also permits .m files with an indefinite number of function stored in them, in which the first executable token of the .m file is not function or classdef and these are called script files. The functions in such files must all have an end matching their function statement. None of the functions in the file can be called directly from outside the file without the cooperation of the code. Functions in such a file have the additional restriction that they cannot have a function name that is the same as the name of the .m file. The script part of the file must precede any functions in the file.
If you do happen to have file that starts with function then MATLAB expects that you are dealing with the first case, a file that contains only functions. If there is no end matching the function line, then MATLAB would have no idea that not all the code is intended to be part of the function. If there is an end matching the function line, then MATLAB would think of any following non-function as being code that was accidentally unreachable (e.g., as might happen if you accidentally include one too many end matching for or if statements.)
So for script files, the script part must come first in the file, and then the functions must be below that and must have an end matching their function statement, and must not be named the same as the file.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Platform and License en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!