Unit-5(Solution of ODE)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 61

Solution of Ordinary

differential Equations
(ODEs)

3-1
2 nd
Order Range-kutta
Method

3-2
3-3
3-4
3-5
MATLAB Program
f=input('Enter the function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate your
solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
for i=1:n
t(i+1)=t0+i*h;
k1=h*f(t(i),y(i));
k2=h*f(t(i+1),y(i)+k1);
y(i+1)=y(i)+(1/2)*(k1+k2);
Coupled Equations

3-7
3-8
MATLAB Program-Coupled
Equations
f=input('Enter the first function:');
g=input('Enter the second function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the 1st dependent
variable:');
z0=input('Enter the initial value of the 1st dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate
your solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
z(1)=z0;
for i=1:n
m1=h*g(t(i),y(i),z(i));
k2=h*f(t(i+1),y(i)+k1,z(i)+m1);
m2=h*g(t(i+1),y(i)+k1,z(i)+m1);
y(i+1)=y(i)+(1/2)*(k1+k2);
z(i+1)=z(i)+(1/2)*(m1+m2);
fprintf('y(%.2f)=%0.4f\n',t(i+1),y(i+1));
fprintf('z(%.2f)=%0.4f\n',t(i+1),z(i+1));
end
4 th
Order Range-kutta
Method

3-11
MATLAB Program
f=input('Enter the function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate your
solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
for i=1:n
t(i+1)=t0+i*h;
k1=h*f(t(i),y(i));
k2=h*f(t(i)+(h/2),y(i)+(k1/2));
k3=h*f(t(i)+(h/2),y(i)+(k2/2));
k4=h*f(t(i)+(h),y(i)+(k3));
MATLAB Program-Coupled
Equations
f=input('Enter the first function:');
g=input('Enter the second function:');
t0=input('Enter the initial value of the independent
variable:');
y0=input('Enter the initial value of the 1st dependent
variable:');
z0=input('Enter the initial value of the 1st dependent
variable:');
h=input('Enter the step size:');
tn=input('Enter point at which you want to evaluate your
solution:');
n=(tn-t0)/h;
t(1)=t0;
y(1)=y0;
z(1)=z0;
for i=1:n
t(i+1)=t0+i*h;
m1=h*g(t(i),y(i),z(i));
k2=h*f(t(i)+(h/2),y(i)+(k1/2),z(i)+(m1/2));
m2=h*g(t(i)+(h/2),y(i)+(k1/2),z(i)+(m1/2));
k3=h*f(t(i)+(h/2),y(i)+(k2/2),z(i)+(m2/2));
m3=h*g(t(i)+(h/2),y(i)+(k2/2),z(i)+(m2/2));
k4=h*f(t(i)+(h),y(i)+(k3),z(i)+(m3));
m4=h*g(t(i)+(h),y(i)+(k3),z(i)+(m3));
y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4);
z(i+1)=z(i)+(1/6)*(m1+2*m2+2*m3+m4);
fprintf('y(%.2f)=%0.4f\n',t(i+1),y(i+1));
fprintf('z(%.2f)=%0.4f\n',t(i+1),z(i+1));
end
ODE Solving using
dsolve

3-18
MATLAB Program

syms y(x) a
eqn = diff(y,x) == y;
S = dsolve(eqn)
syms y(t) a
eqn = diff(y,t,2) == a*y;
ySol(t) = dsolve(eqn)
syms y(t) a
eqn = diff(y,t) == a*y;
cond = y(0) == 5;
ySol(t) = dsolve(eqn,cond)
syms y(t) a b
eqn = diff(y,t,2) == a^2*y;
Dy = diff(y,t);
cond = [y(0)==b, Dy(0)==1];
ySol(t) = dsolve(eqn,cond)
syms y(t) z(t)
eqns = [diff(y,t) == z, diff(z,t) == -y];
S = dsolve(eqns)
Solving First –order
equations using ODE23
and ODE45

3-25
%dy/dx=(x^2-2*x+2)/(y^2); domain [0.5,5] ;y=2 @ x=0.5
syms y(t);
answer=dsolve('Dy=(x^2-2*x+2)/(y^2)','y(0.5)=2','x');
%simplify(answer);
domain=[0.5 5];
fplot(answer,domain);
ODE Solving using
ODE23

3-27
Solving using ode23 and comparing the same with solving
dsolve command
%Solving first order differential equations using ODE23
and ODE45
domain=[0.5 5];
y_at_xi=2;
%ode23('Which is the function that contains the
DE',domain,y_at_xi)
[IV,DV]=ode23('MYODE1',domain,y_at_xi);
disp('IV');
disp('________');
fprintf('\n');
disp(IV);
disp('DV');
disp('________');
fprintf('\n');
disp(DV);
figure;
%dy/dx=(x^2-2*x+2)/(y^2); domain [0.5,5] ;y=2 @
x=0.5
syms y(t);
answer=dsolve('Dy=(x^2-2*x+2)/(y^2)','y(0.5)=2','x');
fplot(answer,domain);
hold on;
plot(IV,DV,'r.');
clc;
clear all;
close all;
%Solve first order De using ODE23
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode23(F,[0,3],1);
plot(x,y,'r--','Linewidth',2);
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE23');
ODE Solving using
ODE45

3-31
clc;
clear all;
close all;
%Solve first order De using ODE45
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode45(F,[0,3],1);
plot(x,y,'r--','Linewidth',2);
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE45');
clc;
clear all;
close all;
%Solve first order De using ODE23
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode23(F,[0,3],1);
figure;
plot(x,y,'r--','Linewidth',2);
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE23');
hold on;
clc;
clear;
%Solve first order De using ODE45
%define the function dydx=-2x^3-x+y
F=@(x,y) (-2*x.^3-x+y);
[x,y]=ode45(F,[0,3],1);
plot(x,y,'k.');
xlabel('x');
ylabel('y');
title('Solving First order DE using ODE45');
Graphical User
Interface

3-35
MATLAB Graphical User Interface

A graphical user interface (GUI) is a visual interface to a program.


A good GUI can make applications more comfortable to use by
providing them with a consistent appearance and with intuitive
controls such as pushbuttons, edit boxes, list boxes, sliders, and
menus.

How does Graphical User Interface Work?


A graphical user interface provides the client with a familiar
environment in which to work. It contains push buttons, toggle
buttons, lists, menus, text boxes, etc. GUIs are harder for the
programmer because a GUI-based application must be prepared
The three principal components required to create a MATLAB
graphical user interface are:

Components

Each element on a MATLAB GUI (pushbuttons, labels, edit boxes,


etc.) is a graphical component. The method of components
includes graphical controls (pushbuttons, toggle buttons, edit
boxes, files, sliders, etc.) static components (text boxes), menus,
toolbars, and axes. The function uicontrol creates graphical
controls and text boxes, and menus are created by the functions
uimenu and uicontext menu. Toolbars are organized by function
uitoolbar. Axes, which are used to show graphical data, are
Containers
The components of a GUI must be organized within a container,
which is a window on the computer screen. The most common
container is a figure. A figure is a window on the laptop screen
that has a title bar along the top, and that can optionally have
menus connect. In the past, pictures have been created
automatically whenever we plotted data.
Empty figures can be generated with the function figure, and
they can be used to influence any combination of components
and other containers. The different types of containers are panels
(created by the function uipanel) and button groups (created by
the function uibutton group). Panels can involve components or
Callbacks

Finally, there must be some way to act if a user clicks a mouse on


a button or type information on a keyboard. A mouse clicks or a
key press is an event, and the MATLAB program must respond to
each event if the program is to perform its function. For example,
if a user clicks on a button, then that event must cause the
MATLAB code that implements the role of the button to be
executed. The code performed in response to an event is known
as a callback. There must be a callback to implement the role of
each graphical element on the GUI.
Components of GUI
Example
To implement a simple calculator as a GUI (Graphical
user interface).
% --- Executes on button press in BtnZero.
function BtnZero_Callback (hObject, eventdata, handles)
% hObject handle to BtnZero (see GCBO)
% eventdata reserved - to be described in a future version of
MATLAB
% handles structure with manage and user data (see GUID
ATA)
textString=get (handles. edit1,'string');
textString=strcat(textString,'0');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnOne.


function BtnOne_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'1');
% --- Executes on button press in BtnTwo.
function BtnTwo_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'2');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnThree.


function BtnThree_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'3');
set(handles. edit1,'string', textString);

% --- Executes on button press in BtnFour.


function BtnFour_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'4');
set (handles. edit1,'string',textString);
% --- Executes on button press in BtnFive.
function BtnFive_Callback(hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'5');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnSix.


function BtnSix_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'6');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnSeven.


function BtnSeven_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'7');
set (handles. edit1,'string', textString);
% --- Executes on button press in BtnEight.
function BtnEight_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'8');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnNine.


function BtnNine_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'9');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnPlus.


function BtnPlus_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'+');
set(handles. edit1,'string', textString);
% --- Executes on button press in BtnMinus.
function BtnMinus_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'-');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnMul.


function BtnMul_Callback (hObject, eventdata, handles)
textString=strcat(textString,'*');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnDivide.


function BtnDivide_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'/');
set (handles. edit1,'string', textString);
% --- Executes on button press in BtnCalc.
function BtnCalc_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=eval(textString);
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnClear.


function BtnClear_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
set (handles. edit1,'string','');
MATLAB GUIs (Creating Graphical User Interfaces) (northwestern.edu)

You might also like