0% found this document useful (0 votes)
9 views9 pages

Milne

The document contains MATLAB code for implementing Milne's method and Runge-Kutta method for solving ordinary differential equations. It also includes programs for calculating gradient, divergence, curl, and verifying Green's theorem using symbolic computation. The outputs demonstrate the results of the calculations and the solution curves for given differential equations.

Uploaded by

samgameing1257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Milne

The document contains MATLAB code for implementing Milne's method and Runge-Kutta method for solving ordinary differential equations. It also includes programs for calculating gradient, divergence, curl, and verifying Green's theorem using symbolic computation. The outputs demonstrate the results of the calculations and the solution curves for given differential equations.

Uploaded by

samgameing1257
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

%Milne's method

clear;
clc;
f = input('Enter your dy/dx : \n'); %Enter the Function with @(x,y)
x0 = input('Enter the Initial Value of x : \n');
y0 = input('Enter the Initial Value of y : \n');
xn = input('Enter the Value of x at which y is to be Calculated : \n');
h = input('Enter the Step Size :\n');
n = (xn-x0)/h;

%Runge-Kutta Method
for i = 1:4
k1 = h*f(x0(i),y0(i));
k2 = h*f(x0(i)+(h/2),y0(i)+(k1/2));
k3 = h*f(x0(i)+(h/2),y0(i)+(k2/2));
k4 = h*f(x0(i)+h,y0(i)+k3);
yn(i) = y0(i) + (1/6)*(k1+(2*k2)+(2*k3)+k4);
x0(i+1) = x0(i) + h;
y0(i+1) = yn(i);
end

%Milne's Mathod
for i = 1:4
F(i) = f(x0(i),y0(i));
end
for i = 5:n+1
y0(i) = y0(i-4) + (4*h/3)*(2*F(i-3)-F(i-2)+2*F(i-1))
%Predictor Formula
F(i) = f(x0(i),y0(i))
y0(i) = y0(i-2) + (h/3)*(F(i-2)+4*F(i-1)+F(i))
%Corrector Formula
x0(i+1) = x0(i) + h
end
fprintf('The Solution is : %f\n',y0(n+1))
OUTPUT

Enter your dy/dx : @(x,y) x+y^2

Enter the Initial Value of x : 0

Enter the Initial Value of y : 1

Enter the Value of x at which y is to be Calculated : 0.4

Enter the Step Size : 0.1

y0 = 1.0000 1.1165 1.2736 1.4880 1.7866

F = 1.0000 1.3466 1.8220 2.5142 3.5920

y0 = 1.0000 1.1165 1.2736 1.4880 1.7893

x0 = 0 0.1000 0.2000 0.3000 0.4000 0.5000

The Solution is : 1.789252


%Program:1
%Find gradient of f(x,y,z) = x.^3 + y.^3 + z.^3 + 3*x*y*z
syms x y z
f(x,y,z) = x.^3 + y.^3 + z.^3 + 3*x*y*z;
V = [x,y,z];
gf = gradient(f,V)

%Program:2
%Find Divergence of f = xi + 2*y^2 + 3*z^3k
syms x y z
F = [x 2*y^2 3*z^3];
V = [x y z];
dF = divergence(F,V)

%Program:3
%Find Curl of x^3*y^2*z+y^3*z^2*x+z^3*x^2*y
syms x y z
F = [x^3*y^2*z, y^3*z^2*x, z^3*x^2*y];
V = [x y z];
CV = curl(F,V)

%Program:4
%Prove that F = (x+3y)i + (y-2z)j + (x-2z)k is Solenoidal
syms x y z
F = [x+3*y y-2*z x-2*z];
V = [x y z];
dF = divergence(F,V)

%Program:5
%Prove that vector F = (2*x*y^3*z^4)i + (3*x^2*y^2*z^4)j + (4*x^2*y^3*z^3)k
%is irrotational.
syms x y z
F = [2*x*y^3*z^4, 3*x^2*y^2*z^4, 4*x^2*y^3*z^3];
V = [x y z];
CV = curl(F,V)
OUTPUT

gf(x, y, z) = 3*x^2 + 3*y*z

3*y^2 + 3*x*z

3*z^2 + 3*x*y

dF = 9*z^2 + 4*y + 1

CV = x^2*z^3 - 2*x*y^3*z

x^3*y^2 - 2*x*y*z^3

- 2*x^3*y*z + y^3*z^2

dF = 0

CV = 0

0
syms x y
F = input('Enter the vector field in the form [M N] :');
y1 = input('Enter the first curve in terms of x :');
y2 = input('Enter the second curve in terms of x :');
x_ip = input('Enter the x limit in the form [x1 x2] :');
dy = diff(y1,x);
f = subs(F, y,y1);
integrand1 = f(1,1)+f(1,2)*dy;
I1 = int(integrand1,x,x_ip(1,1),x_ip(1,2))
y2 = x^2;
f = subs(F,y,y2);
dy = diff(y2,x);
integrand2 = f(1,1)+f(1,2)*dy;
I2 = int(integrand1,x,x_ip(1,2),x_ip(1,1))
%Result
disp('LHS of greens theorem I1 +I2');
LHS = I1 +I2
M = F(1);
N = F(2);
f = diff(N,x) - diff(M,y);
ymin=y1;
ymax=y2;
xmin=x_ip(1,1);
xmax=x_ip(1,2);
RHS = int(int(f,y,ymin(1),ymax(1)),x,xmin(1),xmax(1));
disp('RHS of greens theorem I1 +I2');
RHS
if LHS==RHS
disp('LHS=RHS')
disp('hence, the Greens Theorem is verified')
end
OUTPUT

Enter the vctor field in the form [M N] :[x*y+y^2 x^2]

Enter the first curve in terms of x :y

Enter the second curve in terms of x :sqrt(y)

Enter the x limit in the form [x1 x2] :[0,1]

I1 = (y*(2*y + 1))/2

I2 = -(y*(2*y + 1))/2

LHS of greens theorem I1 +I2

LHS = 0

RHS of greens theorem I1 +I2

RHS = y^2 - y/2 + 1/20


clear;
clc;
f = input('Enter your dy/dx : \n'); %Enter the Function with @(x,y)
x0 = input('Enter the Initial Value of x : \n');
y0 = input('Enter the Initial Value of y : \n');
xn = input('Enter the Value of x at which y is to be Calculated : \n');
h = input('Enter the Step Size :\n');
n = (xn-x0)/h;

%Runge-Kutta Method
for i = 1:4
k1 = h*f(x0(i),y0(i));
k2 = h*f(x0(i)+(h/2),y0(i)+(k1/2));
k3 = h*f(x0(i)+(h/2),y0(i)+(k2/2));
k4 = h*f(x0(i)+h,y0(i)+k3);
yn(i) = y0(i) + (1/6)*(k1+(2*k2)+(2*k3)+k4);
x0(i+1) = x0(i) + h;
y0(i+1) = yn(i);
end
sol = [x0',y0']
plot(x0,y0)
title('Solution curve')
OUTPUT

Enter your dy/dx : @(x,y) x+y

Enter the Initial Value of x : 0

Enter the Initial Value of y : 1

Enter the Value of x at which y is to be Calculated : 0.4

Enter the Step Size : 0.1

sol = 0 1.0000

0.1000 1.1103

0.2000 1.2428

0.3000 1.3997

0.4000 1.5836

You might also like