Professor Provided this code but I cant get it to run

1 view (last 30 days)
This is the exact code he gave us and I cant get it to run. Supposedly its supposed to look like this:
function RHS = HeatTransferODE ( time , z )
%
% define variables U, T0 , TL , Ta , L as global ,
% to assign numerical values to them once
% outside this function
%
global U Ta
%
% compute right - hand side of vector equations dy/dt = f(t,y(t))
%
RHS = [ z (2); U *( z (1) - Ta )];
% assign numerical values to global variables U, Ta ,
% to be able to use such values in all sub - programs
% that include the global command
%
global U Ta
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 1.1;
%
% guess two values for z2 (0)
z20one = -2700;
z20two = -2800;
%
% call ODE solver , for one initial condition , plot solution , and label plot
%
% guess one
z0 = [ T0 ; z20one ];
[X , Z1 ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
figure (1)
plot (X , Z1 (: ,1) , 'o - ' );
hold on
%
% % guess two
z0 = [ T0 ; z20two ];
[X , Z2 ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
plot (X , Z2 (: ,1) , 's - ');
%
% final guess
z20new = z20two + ( TL - Z2 (end ,1))*( z20two - z20one )/( Z2 (end ,1) - Z1 (end ,1))
z0 = [ T0 ; z20new ]
[X , Z ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
plot (X , Z (: ,1) , '^ - ' ); xlabel ( 'x ' );
ylabel ( ' z_1 ^(^1^)( x ) , ␣ z_1 ^(^2^)( x ) , ␣ z_1 ^(^3^)( x ) ' ); axis tight ;
legend ( ' z_1 ^(^1^)( x ) ' , ' z_1 ^(^2^)( x ) ' , ' z_1 ^(^3^)( x ) ' , ' Location ' , ' northwest ')
hold off
%
figure (2)
plot (X , Z (: ,1) , '^ - ' ); xlabel ( 'x ' ); ylabel ( ' z_1 ( x ) ' ); axis tight ;
title ( ' Final ␣ solution ␣ T ( x ) ␣ in ␣ detail ')

Accepted Answer

Walter Roberson
Walter Roberson on 19 Nov 2020
% assign numerical values to global variables U, Ta ,
% to be able to use such values in all sub - programs
% that include the global command
%
global U Ta
U = 100;
T0 = 350;
TL = 550;
Ta = 75;
L = 1.1;
%
% guess two values for z2 (0)
z20one = -2700;
z20two = -2800;
%
% call ODE solver , for one initial condition , plot solution , and label plot
%
% guess one
z0 = [ T0 ; z20one ];
[X , Z1 ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
figure (1)
plot (X , Z1 (: ,1) , 'o - ' );
hold on
%
% % guess two
z0 = [ T0 ; z20two ];
[X , Z2 ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
plot (X , Z2 (: ,1) , 's - ');
%
% final guess
z20new = z20two + ( TL - Z2 (end ,1))*( z20two - z20one )/( Z2 (end ,1) - Z1 (end ,1))
z20new = -2.7498e+03
z0 = [ T0 ; z20new ]
z0 = 2×1
0.3500 -2.7498
[X , Z ] = ode45 ( @HeatTransferODE , [0 , L ] , z0 );
plot (X , Z (: ,1) , '^ - ' ); xlabel ( 'x ' );
ylabel ( 'z_1^(^1^)(x), z_1^(^2^)(x), z_1^(^3^)(x)' ); axis tight ;
legend ( {'z_1^(^1^)(x)' , 'z_1^(^2^)(x)' , 'z_1^(^3^)(x)'} , 'Location' , ' northwest ')
hold off
%
figure (2)
plot (X , Z (: ,1) , '^-' ); xlabel( 'x' ); ylabel( 'z_1(x)' ); axis tight ;
title ( 'Final solution T(x) in detail')
function RHS = HeatTransferODE ( time , z )
%
% define variables U, T0 , TL , Ta , L as global ,
% to assign numerical values to them once
% outside this function
%
global U Ta
%
% compute right - hand side of vector equations dy/dt = f(t,y(t))
%
RHS = [ z(2); U * ( z(1) - Ta )];
end

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!