Professor Provided this code but I cant get it to run
2 次查看(过去 30 天)
显示 更早的评论
This is the exact code he gave us and I cant get it to run. Supposedly its supposed to look like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/418973/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/418978/image.jpeg)
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 ')
0 个评论
采纳的回答
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))
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')
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!