0% found this document useful (0 votes)
20 views3 pages

Integration Ode Merged

The document contains MATLAB code for numerical integration using Simpson's 1/3 Rule and solving a differential equation using the Runge-Kutta 4th order method. It prompts the user for input values such as limits, intervals, and initial conditions, and calculates the approximate integral and solution. The results are displayed at each step of the calculations.

Uploaded by

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

Integration Ode Merged

The document contains MATLAB code for numerical integration using Simpson's 1/3 Rule and solving a differential equation using the Runge-Kutta 4th order method. It prompts the user for input values such as limits, intervals, and initial conditions, and calculates the approximate integral and solution. The results are displayed at each step of the calculations.

Uploaded by

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

clc;

clear;

% Define the function to integrate (edit this as needed)


f = @(x) x.^2 + 3*x + 2;

% Input lower limit, upper limit, and number of intervals


a = input('Enter the lower limit a: ');
b = input('Enter the upper limit b: ');
n = input('Enter the number of intervals (must be even): ');

% Check if number of intervals is even


if mod(n, 2) ~= 0
error('Number of intervals must be even for Simpson''s 1/3 Rule.');
end

% Calculate step size


h = (b - a) / n;

% Initialize sum with first and last terms


I = f(a) + f(b);

% Apply Simpson's 1/3 Rule


for i = 1:n-1
x = a + i*h;
if mod(i, 2) == 0
I = I + 2 * f(x);
else
I = I + 4 * f(x);
end
end

% Final integral value


I = I * (h/3);

% Display the result


fprintf('Approximate value of the integral = %.6f\n', I);

Approximate value of the integral = 53.333333

1
clc;
clear;

% Define the differential equation dy/dx = f(x, y)


f = @(x, y) -2*y + x^3*exp(-2*x); % Example: dy/dx = x + y

% Initial conditions
x0 = input('Enter initial value of x (x0): ');
y0 = input('Enter initial value of y (y0): ');

% Final value of x and number of steps


xf = input('Enter final value of x (xf): ');
n = input('Enter number of steps n: ');

% Step size
h = (xf - x0) / n;

% Initialize values
x = x0;
y = y0;

% Runge-Kutta 4th order iterations


for i = 1:n
k1 = h * f(x, y);
k2 = h * f(x + h/2, y + k1/2);
k3 = h * f(x + h/2, y + k2/2);
k4 = h * f(x + h, y + k3);

% Update y and x
y = y + (1/6)*(k1 + 2*k2 + 2*k3 + k4);
x = x + h;

% Display intermediate results


fprintf('At x = %.4f, y = %.6f\n', x, y);
end

At x = 0.0200, y = 0.960789
At x = 0.0400, y = 0.923117
At x = 0.0600, y = 0.886923
At x = 0.0800, y = 0.852153
At x = 0.1000, y = 0.818751
At x = 0.1200, y = 0.786669
At x = 0.1400, y = 0.755856
At x = 0.1600, y = 0.726268
At x = 0.1800, y = 0.697859
At x = 0.2000, y = 0.670588

% Final result
fprintf('\nApproximate solution at x = %.4f is y = %.6f\n', x, y);

Approximate solution at x = 0.2000 is y = 0.670588

1
2

You might also like