MATLAB: Matlab invalid expression error

differential equationsmathematics

How do I correct this? I'm writing code for a 2nd order bvp, and have ann invalid expression error on this line.
function dydx = odefun( z, y, wc, (pi*D^2)/4*k, qs, {piDh)*(T-Ta) )
dydx = [ y(2);
( wc * y(2) - qs * y(1) - (piDh)*(T-Ta) ) / piD^2/4*k ];
end
I've tried adjusting the parentheses, not really sure what next to do.

Best Answer

Please check that I extract the correct simulated output. You imply the first output should be extracted but the plot here is from the second output: the first output was around 4e5 for most of the range.
bvpmodel
piD24k = 628.3185
Elapsed time is 0.473840 seconds.
ans = 1×2
1 50
ans = 1×2
1 50
ans = 1×2
1 50
function bvpmodel
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%







% %









% Inputs %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% problem parameters: ====================================================
D=2; % try: 1,3
k=200; %try: 800
% coefficients:
wc = 40.0; % try: 30, 40, 50, 60
piD24k = pi*D^2/4*k
piD24k = 0.009; % try: 0.006, 0.004, 0.0009
qs = 0.0; % try: 0, 1000, 10000
piDhTTa = 3.0; % try: 32300, 6282, 8000, 10000 %piDhTTa is pi*Dh*(T-Ta)
% domain:
L = 2;
% boundary conditions:
T0 = 30;
Ttarget = 200;
% solver parameters: =====================================================
% solution domain:
N = 50; % number of nodes
z = linspace( 0, L, N ); % set z equally spaced over the z domain
% initial guess: (constant values)
Tinit = T0; % T(z) = T0
dTdzinit = 0; % dT/dz(z) = 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Solution %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solution initialization:
solinit = bvpinit( z, [ Tinit; dTdzinit ] );
% solution process - bvp4c solver:
% - the "@" notation is used in function handles; the expression:
% "@( var1, var2, ... )fun( var1, var2, ..., vari, ... varn )"
% allows using variables var1, var2, ... in calls to the function "fun"
% while the values of variables vari ... varn specified prior to the
% call to bvp4c
%piD24k is pi*D^2/4*k


%piDhTTa is pi*Dh*(T-Ta)


tic % start clock for computing time
sol = bvp4c( @( z, y )odefun( z, y, wc, piD24k, qs, piDhTTa),...
@( ya, yb )bcfun( ya, yb, T0, Ttarget ),...
solinit );
toc % end clock for computing time
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Post-Processing %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% retrieve solution:
%T = sol.y( 1, :);
temp = deval(sol, z);
T = temp(2,:);
% analytical solution for qs = 0:
Tanalytic = ( T0 + (Ttarget - T0) .* ( ( exp( wc.*z/(piD24k) ) - 1 )/( exp( wc.*L / (piD24k) ) - 1 ) ) + ( (piDhTTa) ).*L / (piD24k) ) .* ( ( z/L - exp( wc.*z / (piD24k) ) - 1 )/( exp( wc.*L / (piD24k) ) - 1 ) ) ;
%piD24k is pi*D^2/4*k
%piDhTTa is pi*Dh*(T-Ta)
% plots:
figure;
size(z), size(T), size(Tanalytic)
plot( z, T, '-b', z, Tanalytic, '+r');
grid;
box on;
legend( ' T ', ' T_{analytic} (for q_s = 0) ' );
title(' Boundary Value Problem ' );
xlabel( ' z ' );
ylabel( ' T(z) ');
end
function dydx = odefun( z, y, wc, piD24k, qs, piDhTTA )
%piD24k is pi*D^2/4*k
%piDhTTa is pi*Dh*(T-Ta)
dydx = [ y(2);
( wc * y(2) - qs * y(1) - piDhTTA ) / piD24k ];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Function evaluating the boundary conditions: res = F( ya, yb, ... ) %
% %
% For this problem: y = [ y1 y2 ], y1 = T, y2 = dT/dz %
% %
% x = 0: T - T0 = 0 --> y1 - T0 = 0 %
% x = L: T - Ttarget = 0 --> y1 - Ttarget = 0 %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function res = bcfun( ya, yb, T0, Ttarget )
res = [ ya(1) - T0 ;
yb(1) - Ttarget ];
end
Related Question