Professor Provided this code but I cant get it to run

1 次查看(过去 30 天)
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 ')

采纳的回答

Walter Roberson
Walter Roberson 2020-11-19
% 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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by