NM Code
NM Code
C code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,k;
float a = 0, b = 0 ,x[10] ,y[10], sx = 0 ,sy = 0 ,sxy=0, sx2 = 0;
printf("Enter the number of points \n") ;
scanf("%d",&n);
printf("Enter the value of x and fx \n");
for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<n;i++)
{
sx = sx + x[i];
sy = sy + y[i];
sxy = sxy+x[i]*y[i];
sx2 = sx2+x[i] * x[i] ;
}
b=((n*sxy) - (sx*sy))/((n*sx2) - (sx*sx));
a= (sy / n) -(b * sx/n);
printf("Fitted line is: %f + % f x ",a,b);
getch();
}
Python code:
import numpy as np
X = np.array([list(map(float, input().split()))
C code:
#include <stdio.h>
#include <math.h>
#define N 5
#define DEG 2
double coeffs[DEG+1];
coeffs[i] = A[i][n+1];
coeffs[i] /= A[i][i];
printf("Polynomial Coefficients:\n");
int main() {
A[i][j] = X[i+j];
A[i][DEG+1] = B[i];
solve(A, DEG);
return 0;
Python code:
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2.2, 2.8, 3.6, 4.5, 5.1])
C code:
#include <stdio.h>
#include <math.h>
void exponential_regression(int n, double x[], double y[], double *a, double *b) {
sum_x += x[i];
sum_y += log(y[i]);
int main() {
double x[] = {1, 2, 3, 4, 5}, y[] = {2.2, 2.8, 3.6, 4.5, 5.1};
double a, b;
return 0;
Python code:
import numpy as np
def exponential_regression(x, y):
log_y = np.log(y)
return np.exp(log_a), b
x = np.array([1, 2, 3, 4, 5])
a, b = exponential_regression(x, y)
C code:
#include <stdio.h>
#include <math.h>
double f(double x) {
return x * x;
return (h / 2) * sum;
int main() {
double a = 0, b = 1;
int n = 4;
return 0;
Python code:
def f(x):
return x**2
h = (b - a) / n
a, b, n = 0, 1, 4
C code:
#include <stdio.h>
#include <math.h>
double f(double x) {
return x * x;
double h = (b - a) / 2.0;
if (n % 2 != 0) n++;
return (h / 3) * sum;
int main() {
double a = 0, b = 1;
int n = 4;
return 0;
}
Python code:
import numpy as np
def f(x):
return x ** 2
h = (b - a) / 2.0
if n % 2 != 0:
n += 1
h = (b - a) / n
x = np.linspace(a, b, n + 1)
a, b, n = 0, 1, 4
C code:
#include <stdio.h>
#include <math.h>
double f(double x) {
double h = (b - a) / 3.0;
if (n % 3 != 0) n += (3 - n % 3);
return (3 * h / 8) * sum;
int main() {
double a = 1, b = 2;
int n = 6;
return 0;
Python code:
def f(x):
return x**3
h = (b - a) / 3
if n % 3 != 0:
n += (3 - n % 3)
h = (b - a) / n
return (3 * h / 8) * sum_
a, b, n = 1, 2, 6
C code:
#include <stdio.h>
#include <math.h>
int main() {
int n, i, j, k;
scanf("%d", &n);
scanf("%f", &a[i][j]);
scanf("%f", &b[i]);
printf("Method failed\n");
return 1;
x[i] = b[i];
x[i] /= a[i][i];
return 0;
Python code:
import numpy as np
n = len(b)
A, b = A.astype(float), b.astype(float)
x = np.zeros(n)
return x
b = np.array(list(map(float, input().split())))
C output:
#include <stdio.h>
if (k != i) {
int main() {
float matrix[3][4] = {
{-2, 1, 2, -3}
};
gaussJordan(matrix);
}
return 0;
Python code:
def gauss_jordan(matrix):
for i in range(len(matrix)):
diag = matrix[i][i]
for j in range(len(matrix)):
if i != j:
factor = matrix[j][i]
matrix = [
[-2, 1, 2, -3]
gauss_jordan(matrix)
for i in range(len(matrix)):
print(f"x{i+1} = {matrix[i][3]:.2f}")
Matrix Inversion by Gauss Jordan Method
C code:
#include <stdio.h>
#define N 3
inv[i][j] = (i == j);
a[i][j] /= pivot;
inv[i][j] /= pivot;
if (k != i) {
int main() {
float a[N][N] = {{2, 1, 1}, {1, 3, 2}, {1, 0, 0}}, inv[N][N];
gaussJordan(a, inv);
return 0;
Python code:
import numpy as np
def gauss_jordan(a):
n = len(a)
a = np.array(a, dtype=float)
inv = np.eye(n)
for i in range(n):
a[i] /= a[i, i]
inv[i] /= a[i, i]
for k in range(n):
if k != i:
factor = a[k, i]
return inv
inv = gauss_jordan(A)
print(np.round(inv, 2))
Matrix Factorization LU Method
C code:
#include <stdio.h>
u[i][k] = a[i][k];
if (i == k) l[i][i] = 1;
else {
l[k][i] = a[k][i];
l[k][i] /= u[i][i];
int main() {
int n;
scanf("%d", &n);
luDecomposition(n, a, l, u);
printf("Lower Matrix:\n");
printf("Upper Matrix:\n");
return 0;
Python code:
for i in range(n):
u[i][k] = a[i][k]
for j in range(i):
if i == k:
l[i][i] = 1.0
else:
l[k][i] = a[k][i]
for j in range(i):
l[k][i] /= u[i][i]
return l, u
def main():
a = []
for i in range(n):
a.append(row)
l, u = lu_decomposition(n, a)
print("Lower Matrix:")
for row in l:
print("Upper Matrix:")
for row in u:
print(" ".join(f"{val:.2f}" for val in row))
if __name__ == "__main__":
main()
Jacobi Iterative Method
C code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void jacobi(int n, double A[10][10], double b[10], double tol, int max_iter, double x[10]) {
double x_new[10];
int iter;
x[i] = 0.0;
if (j != i) {
x[i] = x_new[i];
return;
x[i] = x_new[i];
int main() {
scanf("%d", &n);
scanf("%lf", &A[i][j]);
scanf("%lf", &b[i]);
return 0;
Python code:
import numpy as np
for _ in range(max_iter):
x_new = (b - (A @ x - np.diag(A) * x)) / np.diag(A)
return x_new
x = x_new
return x
b = np.array(list(map(float, input().split())))
C code:
#include <stdio.h>
#include <math.h>
void gaussSeidel(int n, double A[n][n], double b[n], double x[n], double tol, int max_iter) {
double max_diff = 0;
x[i] = x_new;
int main() {
int n = 4;
double A[4][4] = {{4, -1, 0, 0}, {-1, 4, -1, 0}, {0, -1, 4, -1}, {0, 0, -1, 3}};
printf("Solution:\n");
Python code:
import numpy as np
for _ in range(max_iter):
x_new = np.copy(x)
for i in range(n):
return x_new
x = x_new
return x
A = np.array([[4, -1, 0, 0], [-1, 4, -1, 0], [0, -1, 4, -1], [0, 0, -1, 3]], dtype=float)
C output:
#include <stdio.h>
#include <math.h>
double euler(double (*f)(double, double), double x0, double y0, double h, int n) {
y0 += h * f(x0, y0);
x0 += h;
return y0;
int main() {
int n;
return 0;
Python output:
C code:
#include <stdio.h>
double heun(double (*f)(double, double), double x0, double y0, double h, double xn) {
x0 += h;
return y0;
int main() {
return 0;
Python code:
import numpy as np
f = lambda x, y: x + y
print(heun(f, 0, 1, 0.1, 0.5))
Shooting Method
C code:
#include <stdio.h>
#include <math.h>
double shooting(double x0, double xn, double y0, double yn, double z0, double h) {
while (1) {
y1 = y;
y = y0, z = z2, x = x0;
y2 = y;
z2 += 1;
int main() {
double x0 = 0, xn = 1, y0 = 0, yn = 1, z0 = 1, h = 0.1;
return 0;
Python code:
import numpy as np
while True:
y, z, x = y0, z1, x0
while x < xn:
y, z, x = rk4(y, z, x, h)
y1 = y
y, z, x = y0, z2, x0
while x < xn:
y, z, x = rk4(y, z, x, h)
y2 = y
C code:
#include <stdio.h>
y0 += (k1 + 2 * k2 + 2 * k3 + k4) / 6;
x0 += h;
int main() {
return 0;
Python code:
C code:
#include <stdio.h>
int main() {
h = x2 - x1;
return 0;
Python code:
x1, f1, x2, f2 = map(float, input("Enter x1, f(x1), x2, f(x2): ").split())
h = x2 - x1
C code:
#include <stdio.h>
int main() {
return 0;
Python code:
C code:
#include <stdio.h>
int main() {
int n;
h = x[1] - x[0];
newton_forward_derivative(x, y, n, h);
return 0;
Python code:
import numpy as np
C code:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
h = x[1] - x[0];
return 0;
Python code:
import numpy as np
def newton_backward(x, y):
return y[-1]
C code:
#include <stdio.h>
#include <math.h>
#define f(x) (x * x)
double R[n][n], h = b - a;
h /= 2.0;
int main() {
double a = 0, b = 1;
int n = 4;
Python code:
import numpy as np
def f(x):
return x ** 2
R = np.zeros((n, n))
h=b-a
h /= 2.0
return R[n - 1, n - 1]
a, b, n = 0, 1, 4
c code:
#include <stdio.h>
term *= (double)x / i;
res += term;
return res;
int main() {
int x = 2, n = 10;
return 0;
Python code:
res, term = 1, 1
term *= x / i
res += term
return res
x, n = 2, 10
C code:
#include <stdio.h>
#include <math.h>
#define f(x, y) (x + y)
y = y0 + h * (x0 + y0);
x += h;
y0 = y;
int main() {
double x0 = 0, y0 = 1, h = 0.1;
int n = 5;
return 0;
Python code:
x, y = x0, y0
print(f"x = {x:.4f}, y = {y:.4f}")
for _ in range(n):
y = y0 + h * (x0 + y0)
x += h
y0 = y
picard(x0, y0, h, n)
Laplace Method
C code:
#include <stdio.h>
#include <math.h>
#define N 10
int i, j;
do {
maxDiff = 0.0;
}
printf("\n");
int main() {
solve_laplace(grid);
print_grid(grid);
return 0;
Python code:
import numpy as np
def solve_laplace(grid):
while True:
new_grid = grid.copy()
new_grid[1:-1, 1:-1] = 0.25 * (grid[:-2, 1:-1] + grid[2:, 1:-1] + grid[1:-1, :-2] + grid[1:-1, 2:])
break
grid = new_grid
return grid
grid = solve_laplace(grid)
print(grid)
Poisson Method
C code:
#include <stdio.h>
#include <math.h>
#define N 50
error = 0.0;
u[i][j] = u_new[i][j];
int main() {
solve_poisson(u, f);
printf("\n");
return 0;
Python code:
import numpy as np
def solve_poisson():
for _ in range(MAX_ITER):
break
u = u_new
return u
u = solve_poisson()
C code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int n;
} Spline;
Spline *s = (Spline*)malloc(sizeof(Spline));
s->x = x;
s->n = n;
s->a[i] = y[i];
}
s->a[n - 1] = y[n - 1];
return s;
int main() {
int n = 4;
free_spline(s);
return 0;
Python code:
import numpy as np
n = len(x)
h = np.diff(x)
alpha = np.zeros(n)
x = [0, 1, 2, 3]
y = [1, 2, 0, 2]
splines = cubic_spline(x, y)