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:
![](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에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!