Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

shooting.m

%
% Shooting method example of solving boundary value ODE
%
%     d^2 T   
%     -----  + c (Tinf - T) = 0
%      dx^2
%
% which models the temperature distribution in a rod
%
% Will be solved using Euler's method

% define constants

Ta   = 40;      % left side temperature
Tb   = 200;     % right side temperature
Tinf = 20;      % ambient temperature
c    = 0.01;    % constant

L      = 10;    % length of bar
x      = 0;     % start position
deltax = 0.01;  % increment in position for numerical procedure

T = Ta;         % initial condition
z = input('Enter starting slope: ');

count = 1;
Tvect(count) = T;
xvect(count) = x;

while x < L
  dTdx = z;
  dzdx = -c * (Tinf - T);

  T = T + dTdx * deltax;  % Euler's method
  z = z + dzdx * deltax;

  x = x + deltax;

  count = count + 1;
  xvect(count) = x;       % store values for plotting
  Tvect(count) = T;
end;

fprintf('The end temperature is: %f\n',T);

plot(xvect,Tvect);
Skip to content