HOW DO I DO THAT IN MATLAB SERIES?
In this series, I am answering questions that students have asked me about MATLAB. Most of the questions relate to a mathematical procedure.
Contents
TOPIC
How do I solve an ordinary differential equation
SUMMARY
Language : Matlab 2010a; Authors : Autar Kaw and Sri Harsha Garapati; Mfile available at Last Revised : January 23, 2012; Abstract: This program shows you how to solve an ordinary differential equation in MATLAB
clc
clear all
INTRODUCTION
disp('ABSTRACT') disp(' This program shows you how to solve an ordinary differential equation') disp(' ') disp('AUTHOR') disp(' Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com') disp(' ') disp('MFILE SOURCE') disp(' http://numericalmethods.eng.usf.edu/blog/calc_operations_ode_blog.m') disp(' ') disp('LAST REVISED') disp(' January 23, 2012') disp(' ')
ABSTRACT This program shows you how to solve an ordinary differential equation AUTHOR Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com MFILE SOURCE http://numericalmethods.eng.usf.edu/blog/calc_operations_ode_blog.m LAST REVISED January 23, 2012
INPUTS
% enter differential equation as string eqn3='3*Dy+6*y=exp(-x)'; % initial condition inco='y(0)=6';
DISPLAYING INPUTS
disp(' ') disp('INPUTS') % printing the the input ordinary differential equation with initial % condition fprintf(' equation to solve: %s\n with: %s\n\n',eqn3,inco)
INPUTS equation to solve: 3*Dy+6*y=exp(-x) with: y(0)=6
THE CODE
% "dsolve" is the command to solve the ordinary differential equation % enter all as strings!!! soln =dsolve(eqn3,inco,'x'); % convert the solution to a string to print in the outputs section solnd=char(vpa(soln,4));
DISPLAYING OUTPUTS AND PLOTS
disp(' ') disp('OUTPUTS') % printing the outputs fprintf(' The solution is: y=%s\n\n',solnd)
OUTPUTS The solution is: y=0.3333/exp(1.0*x) + 5.667/exp(2.0*x)