Can anyone tell whats the problem with this code
MATLAB: Invalid expression error when i execute code
error invalid
Related Solutions
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.
bvpmodelpiD24k = 628.3185Elapsed 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*kpiD24k = 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) ');endfunction 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
cos(x/2)^2
Best Answer