Inputing a function into another function: left and right sides have different elements
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am quite new to matlab, so hopefully someone can spot my errors!
I have a water function that depends on pressure. The pressure is given a function that changes with time (called Pressure_function). The coefficients are stored in an array called c. This is the pressure function:
function p = Pressure_function(t,c)
p = c(:,1)*t.^8 + c(:,2)*t.^7 + c(:,3)*t.^6 + c(:,4)*t.^5 + c(:,5)*t.^4 + c(:,6)*t.^3 + c(:,7)*t.^2 + c(:,8)*t + c(:,9);
p = p*10^5; %convert to [Pa]
end
Now, I have the water function (which is based off a CSTR in series model at steady state). When the pressure is constant, the function works perfectly. However, I want to make this function transient by inputting the pressure function, since it changes with time. I thougt this would be relatively straightforward (see below). However, I continue to end up with the error 'left and right side have different elements'. I'm not sure how to fix this, so any help would be appreciated!
function [vw] = Water_ss_func(t, ps)
p = @Pressure_function;
%Preallocate (ps.nr is the number of CSTR in series - stored in my struct)
vw = zeros(1,ps.nr);
vw(1) = ps.fv_in - (ps.k *ps.area * p(t,c))/ps.c_w;
for i = 2:ps.nr
vw(i) = vw(i-1) - (ps.k *ps.area * p(t,c))/ps.c_w;
end
end
4 commentaires
Uday Pradhan
le 15 Oct 2020
Hi Isabella,
Since t_exp is a 145 x 1 vector, the line:
vw(1) = ps.fv_in - (ps.k *ps.area * p(t,c))/ps.c_w;
is causing an error as the sizes of the two sides are not the same. p(t_exp,c) will also return a 145 x 1 vector and we cannot assign this to vw(1) since it's a scalar. May be you want to evaluate Water_ss_func at each time step? Something like:
Water_ss_func(t_exp(10), ps)
Réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!