Docsity

Solving a Differential Equation using MATLAB: CMSC/AMSC 460 Homework 5 - Prof. Dianne P. O, Assignments of Mathematics

Toggle sidebar
bg1
% CMSC/AMSC 460 Fall 2007
% Homework 5
%
% Purpose: Practice in solving linear systems
% of equations and systems of ODEs.
% We want to use ode45 to solve the differential equation:
% y' = J\y-y
%
% Dianne O'Leary
% Modified by Sima Taheri, 15 Oct. 2007
%
% Parameters:
% [tout,yout,te,ye] = ode45(@hmwk5f, tspan, y0, options);
%
% hmwk5f: A function handle that evaluates the
% right side of the differential equations.
% tspam: A vector specifying the interval of integration, [0,10].
% y0: A vector of initial conditions, y0 = [1,3,4]'.
% options: Structure of optional parameters that change
% the default integration properties.
% hmwk5e: A function handle for event.
%
% Outputs:
% tout: Column vector of time points.
% ypout: Solution array. Each row in yout corresponds to the
% solution at a time returned in the corresponding row of tout
% te: The time when event, y3(t) = 1, occurs.
% ye: The solution at the time of the event
% Plot of the three components of y.
global maxcond
% maxcond records the maximum condition number of the J
% matrix over the course of the integration.
maxcond = 1;
options=odeset('Events',@hmwk5e);
[tout,yout,te,ye] = ode45(@hmwk5f, [0 10], [1 3 4]',options);
plot(tout,yout(:,1),'r-',tout,yout(:,2),'b:',tout,yout(:,3),'g-.')
grid on
xlabel('t')
ylabel('y')
legend('y(1)','y(2)','y(3)')
title('The solution to the differential equation in Hmwk 5')
disp(sprintf('The maximum condition number of J was %f.',maxcond))
disp(sprintf('y(3)=1 at t=%f',te))
%%%%%%%%%%%%%%%%%%%%%%%%%%
pf3

Partial preview of the text

Download Solving a Differential Equation using MATLAB: CMSC/AMSC 460 Homework 5 - Prof. Dianne P. O and more Assignments Mathematics in PDF only on Docsity!

% CMSC/AMSC 460 Fall 2007 % Homework 5 % % Purpose: Practice in solving linear systems % of equations and systems of ODEs. % We want to use ode45 to solve the differential equation: % y' = J\y-y % % Dianne O'Leary % Modified by Sima Taheri, 15 Oct. 2007 % % Parameters: % [tout,yout,te,ye] = ode45(@hmwk5f, tspan, y0, options); % % hmwk5f: A function handle that evaluates the % right side of the differential equations. % tspam: A vector specifying the interval of integration, [0,10]. % y0: A vector of initial conditions, y0 = [1,3,4]'. % options: Structure of optional parameters that change % the default integration properties. % hmwk5e: A function handle for event. % % Outputs: % tout: Column vector of time points. % ypout: Solution array. Each row in yout corresponds to the % solution at a time returned in the corresponding row of tout % te: The time when event, y3(t) = 1, occurs. % ye: The solution at the time of the event % Plot of the three components of y.

global maxcond

% maxcond records the maximum condition number of the J % matrix over the course of the integration.

maxcond = 1;

options=odeset('Events',@hmwk5e); [tout,yout,te,ye] = ode45(@hmwk5f, [0 10], [1 3 4]',options);

plot(tout,yout(:,1),'r-',tout,yout(:,2),'b:',tout,yout(:,3),'g-.') grid on xlabel('t') ylabel('y') legend('y(1)','y(2)','y(3)') title('The solution to the differential equation in Hmwk 5')

disp(sprintf('The maximum condition number of J was %f.',maxcond)) disp(sprintf('y(3)=1 at t=%f',te))

% Function that evaluates the right side of the differential equation. function yprime = hmwk5f(t,y)

global maxcond

J = [1+t t^2y(2) ty(3) ty(1) 1+t t^2y(3) t^2y(1) ty(2) 1+t];

yprime = J\y - y;

maxcond = max(maxcond,cond(J));

% Event function function [value,isterminal,direction] = hmwk5e(t,y)

% value: The value of the function % isterminal = 1, if the integration is to terminate % at a zero of this event function and 0 otherwise. % direction = 0 if all zeros are to be computed % +1 if only the zeros where the event function increases, % -1 if only the zeros where the event function decreases.

value = y(3)-1; isterminal = 0; direction = 0;