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
28 days ago44 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
 3 ODE

f = @(x, y) x + y;
[x, y] = ode45(f, [0 1], 1);
plot(x, y)
title('ode45 Solution')
grid on
4  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);
5  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);

6. Trapezoidal
clc;
clear;
x = [0 1 2 3 4];             
y = [1 2 4 8 16];             
n = length(x);               
h = x(2) - x(1);             
sum = 0;                      
for i = 2:n-1
    sum = sum + y(i);        
end
I = (h/2) * (y(1) + y(n) + 2*sum);  
fprintf('The integral using Trapezoidal Rule = %.4f\n', I);
7.Guass elimination
clc;
clear;

A=[2 1 -1;
   -3 -1 2;
   -2 1 2];

b=[8;-11;-3];
aug=[A b];
n=3;

for i=1:n-1
    for j=i+1:n
        factor=aug(j,i)/aug(i,i);
        aug(j,:)=aug(j,:)-factor*aug(i,:);
    end
end

x=zeros(n,1);

for i=n:-1:1
    sum=0;
    for j=i+1:n
        sum=sum+aug(i,j)*x(j);
    end
    x(i)=(aug(i,end)-sum)/aug(i,i);
end

fprintf('x=%f\n',x(1));
fprintf('y=%f\n',x(2));
fprintf('z=%f\n',x(3));
8 Guass Siedal method
clc;
clear;

A=[4,-1,0;
   -1,4,-1;
   0,-1,4];

b=[15;10;10];

x=[0;0;0];
n=10;

for k=1:n
    x_old=x;

    x(1)=(b(1)-A(1,2)*x(2)-A(1,3)*x(3))/A(1,1);
    x(2)=(b(2)-A(2,1)*x(1)-A(2,3)*x(3))/A(2,2);
    x(3)=(b(3)-A(3,1)*x(1)-A(3,2)*x(2))/A(3,3);

    if norm(x-x_old,inf)<1e-6
        break;
    end
end

fprintf('x=%f\n',x(1));
fprintf('y=%f\n',x(2));
fprintf('z=%f\n',x(3));
9 Linear Fit Quadarate
clc;
close all;

x = [1 2 3 4 5 6 7];
y = [2.4 3.9 6.2 7.8 10.5 13.1 14.8];

xfit = linspace(min(x), max(x), 100);

p1 = polyfit(x, y, 1);
yfit1 = polyval(p1, xfit);

p2 = polyfit(x, y, 2);
yfit2 = polyval(p2, xfit);

plot(x, y, 'ro', 'MarkerFaceColor', 'r');
hold on;

plot(xfit, yfit1, 'b', 'LineWidth', 2);
plot(xfit, yfit2, 'g', 'LineWidth', 2);

xlabel('X Values');
ylabel('Y Values');
title('Comparison of Linear and Quadratic Curve Fitting');
legend('Given Data', 'Linear Fit', 'Quadratic Fit');
grid on;

fprintf('Linear Fit: y = %.4fx + %.4f\n', p1(1), p1(2));
fprintf('Quadratic Fit: y = %.4fx^2 + %.4fx + %.4f\n', p2(1), p2(2), p2(3)); 
← Back to timeline