Professor Provided this code but I cant get it to run

조회 수: 1 (최근 30일)
Will Jeter
Will Jeter 2020년 11월 19일
답변: Walter Roberson 2020년 11월 19일
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에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by