JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
15 days ago58 views
👨‍💻Programming

1. Euler’s Method

f = @(x, y) x + y;
x0 = 0;     
y0 = 1;    
h = 0.1;     
xn = 1;      
x = x0:h:xn;
y = zeros(size(x));
y(1) = y0;
for i = 1:length(x)-1
    y(i+1) = y(i) + h * f(x(i), y(i));
end
plot(x, y, '-o')
title('Euler Method')
grid on

2.Runge Kutta


f = @(x, y) x + y;
x0 = 0;
y0 = 1;
h = 0.1;
xn = 1;
x = x0:h:xn;
y = zeros(size(x));
y(1) = y0;
for i = 1:length(x)-1
    k1 = f(x(i), y(i));
    k2 = f(x(i) + h/2, y(i) + h*k1/2);
    y(i+1) = y(i) + h * k2;
end
plot(x, y, '-o')
title('RK2 Method')
grid on

ODE


f = @(x, y) x + y;
[x, y] = ode45(f, [0 1], 1);
plot(x, y)
title('ode45 Solution')
grid on

simpsons


clc;
clear;
x = [0 1 2 3 4];             
y = [1 2 4 8 16];            
n = length(x);
if mod(n-1,2) ~= 0
    error('Number of intervals must be even for Simpson''s 1/3 rule');
end
h = x(2) - x(1);             
sum_odd = 0;                 
sum_even = 0;                
for i = 2:n-1
    if mod(i,2) == 0
        sum_odd = sum_odd + y(i);    
    else
        sum_even = sum_even + y(i);  
    end
end
I = (h/3) * (y(1) + y(n) + 4*sum_odd + 2*sum_even);
fprintf('The integral using Simpson''s Rule = %.4f\n', I);









 Numerical Differentiation 
clc;
clear;
x = [1 2 3 4 5];
y = [1 4 9 16 25];   
h = x(2) - x(1);     
dy_forward = (y(2) - y(1)) / h;
dy_backward = (y(end) - y(end-1)) / h;
i = 3; 
dy_central = (y(i+1) - y(i-1)) / (2*h);
fprintf('Forward Difference = %.4f\n', dy_forward);
fprintf('Backward Difference = %.4f\n', dy_backward);
fprintf('Central Difference = %.4f\n', dy_central);

← Back to timeline