Numerical Method Assignment
Submitted to Dr. Md. Jashim Uddin
Submitted by Ashikul Islam(173-15-10412)
Section(C)
Problem-1:Solve the following system using Gauss-Seidel or Jacobi
iterativeMethod
27𝑥1−2𝑥2−3𝑥3=30
5𝑥1+30𝑥2−8𝑥3=−11
−2𝑥1−2𝑥2+9𝑥3=−19
Solution:
packagedemo;
import java.util.Scanner;
public class DemoTranslation {
public static void main(String[] args) {
int m = 0;
int n = 0;
int l = 0;
float[] x = null;
float[] b = null;
float[] c = null;
float[][] a = null;
final int posLine= 1;
for(intpos = 0; true;) switch(pos) {
default:
c = new float[10];
b = new float[10];
x = new float[10];
a = new float[10][10];
System.out.println("nEnter thevalue of n : ");
n = STDIN_SCANNER.nextInt();
System.out.println("nEnter thenumber of iterations : ");
l = STDIN_SCANNER.nextInt();
System.out.println("nEnter theright hand side constants : ");
for(inti = 0; i < n; i++) {
b[i] = STDIN_SCANNER.nextFloat();
}
System.out.println("nEnter thecoefficients row wise: ");
for(inti = 0; i < n; i++) {
x[i] = 0;
for(intj = 0; j < n; j++) {
a[i][j] = STDIN_SCANNER.nextFloat();
}
}
m = 1;
case posLine:
for(inti = 0; i < n; i++) {
c[i] = b[i];
for(intj = 0; j < n; j++) {
if(i != j) {
c[i] = c[i] - a[i][j] * x[j];
}
}
}
for(inti = 0; i < n; i++) {
x[i] = c[i] / a[i][i];
}
m++;
if(m <= l) {
pos = posLine;
continue;
} else {
System.out.println("nTheSolution is : ");
for(inti = 0; i < n; i++) {
System.out.printf("nx(%d) =%fn", i, x[i]);
}
}
return;
}
}
public final static Scanner STDIN_SCANNER=new Scanner(System.in);
} }
}
So, x1= 1, x2= -2, x3= -3
Problem-2:The following values of 𝑥 and 𝑦 are given
𝑥 1.5 5.5 8.5 13.5 15.5
𝑦(𝑥) 4.328 7.097 11.94
4
15.87
5
20.89
6
Find 𝑦(2.5)
Solution:
#include<stdio.h>
#define MAXN100
#define ORDER4
int main()
{
float ax[MAXN+1], ay [MAXN+1], diff[MAXN+1][ORDER+1], nr=1.0,
dr=1.0,x,p,h,yp;
int n,i,j,k;
printf("nEnter the value of n: ");
scanf("%d",&n);
printf("nEnter the values in formx,y:n");
for (i=0;i<=n;i++)
scanf("%f %f",&ax[i],&ay[i]);
printf("nEnter the value of x for which the value of y is wanted: n");
scanf("%f",&x);
h=ax[1]-ax[0];
for (i=0;i<=n-1;i++)
diff[i][1] = ay[i+1]-ay[i];
for (j=2;j<=ORDER;j++);
for(i=0;i<=n-j;i++)
diff[i][j] = diff[i+1][j-1] - diff[i][j-1];
i=0;
while (!(ax[i]>x))
i++;
i--;
p = (x-ax[i])/h;
yp = ay[i];
for (k=1;k<=ORDER;k++)
{ nr *=p-k+1;
dr *=k;
yp +=(nr/dr)*diff[i][k];
} printf("nWhen x= %6.1f, corresponding y =%6.2fn",x,yp);
}
So, y(2.5)= 5.27
Problem-3:Certain corresponding values of 𝑥 and log10𝑥 are
(11,1.04139),(17,1.23044),(20,1.301029),(23,1.361727)
Estimates the value of log1015 and find the error.
Solution:
packagedemo;
import java.util.Scanner;
public class DemoTranslation {
public static void main(String[] args) {
float[] x = new float[100], y = new float[100];
float a, s = 1, t = 1, k = 0;
int n;
boolean d = true;
System.out.println("nEnter thenumber of the terms of the table: ");
n = STDIN_SCANNER.nextInt();
System.out.println("nEnter therespectivevalues of the variables x
and y: ");
for(inti = 0; i < n; i++) {
x[i] = STDIN_SCANNER.nextFloat();
y[i] = STDIN_SCANNER.nextFloat();
}
System.out.println("nThetableyou entered is as follows :");
for(inti = 0; i < n; i++) {
System.out.printf("%.6ft%.6f", x[i], y[i]);
System.out.println();
}
while(d) {
System.out.println(" nEnter the value of the x to find the
respective value of y: ");
a = STDIN_SCANNER.nextFloat();
for(inti = 0; i < n; i++) {
s = 1;
t = 1;
for(intj = 0; j < n; j++) {
if(j != i) {
s = s * (a - x[j]);
t = t * (x[i] - x[j]);
}
}
k = k + (s / t) * y[i];
}
System.out.printf("nTherespectivevalueof the variable y is:
%fn", k);
}
}
public final static Scanner STDIN_SCANNER=new Scanner(System.in);
}
So, y(15)= 1.175639
Problem-4:Using Newton divided difference formula, find 𝑓(5) from
the followingfunctional values
𝑓(3)=160,𝑓(9)=392,𝑓(13)=952,𝑓(18)=1300,𝑓(23)=1900
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[10], y[10], p[10];
int k,f,n,i,j=1,f1=1,f2=0;
printf("nEnter the number of observations: n”);
scanf("%d", &n);
printf("nEnter the different values of x:n");
for (i=1;i<=n;i++)
scanf("%d", &x[i]);
printf("nThecorresponding values of y are:n");
for (i=1;i<=n;i++)
scanf("%d", &y[i]);
f=y[1];
printf("nEnter the value of 'k' in f(k) you want to evaluate: n");
scanf("%d", &k);
do
{
for (i=1;i<=n-1;i++)
{
p[i] = ((y[i+1]-y[i])/(x[i+j]-x[i]));
y[i]=p[i];
}
f1=1;
for(i=1;i<=j;i++)
{
f1*=(k-x[i]);
}
f2+=(y[1]*f1);
n--;
j++;
}
while(n!=1);
f+=f2;
printf("nf(%d) =%d", k , f);
getch();
}
So, f(5)= 92

Numerical Method Assignment

  • 1.
    Numerical Method Assignment Submittedto Dr. Md. Jashim Uddin Submitted by Ashikul Islam(173-15-10412) Section(C)
  • 2.
    Problem-1:Solve the followingsystem using Gauss-Seidel or Jacobi iterativeMethod 27𝑥1−2𝑥2−3𝑥3=30 5𝑥1+30𝑥2−8𝑥3=−11 −2𝑥1−2𝑥2+9𝑥3=−19 Solution: packagedemo; import java.util.Scanner; public class DemoTranslation { public static void main(String[] args) { int m = 0; int n = 0; int l = 0; float[] x = null; float[] b = null; float[] c = null; float[][] a = null; final int posLine= 1; for(intpos = 0; true;) switch(pos) { default: c = new float[10]; b = new float[10];
  • 3.
    x = newfloat[10]; a = new float[10][10]; System.out.println("nEnter thevalue of n : "); n = STDIN_SCANNER.nextInt(); System.out.println("nEnter thenumber of iterations : "); l = STDIN_SCANNER.nextInt(); System.out.println("nEnter theright hand side constants : "); for(inti = 0; i < n; i++) { b[i] = STDIN_SCANNER.nextFloat(); } System.out.println("nEnter thecoefficients row wise: "); for(inti = 0; i < n; i++) { x[i] = 0; for(intj = 0; j < n; j++) { a[i][j] = STDIN_SCANNER.nextFloat(); } } m = 1; case posLine: for(inti = 0; i < n; i++) { c[i] = b[i]; for(intj = 0; j < n; j++) { if(i != j) { c[i] = c[i] - a[i][j] * x[j];
  • 4.
    } } } for(inti = 0;i < n; i++) { x[i] = c[i] / a[i][i]; } m++; if(m <= l) { pos = posLine; continue; } else { System.out.println("nTheSolution is : "); for(inti = 0; i < n; i++) { System.out.printf("nx(%d) =%fn", i, x[i]); } } return; } } public final static Scanner STDIN_SCANNER=new Scanner(System.in); } } }
  • 5.
    So, x1= 1,x2= -2, x3= -3
  • 6.
    Problem-2:The following valuesof 𝑥 and 𝑦 are given 𝑥 1.5 5.5 8.5 13.5 15.5 𝑦(𝑥) 4.328 7.097 11.94 4 15.87 5 20.89 6 Find 𝑦(2.5) Solution: #include<stdio.h> #define MAXN100 #define ORDER4 int main() { float ax[MAXN+1], ay [MAXN+1], diff[MAXN+1][ORDER+1], nr=1.0, dr=1.0,x,p,h,yp; int n,i,j,k; printf("nEnter the value of n: "); scanf("%d",&n); printf("nEnter the values in formx,y:n"); for (i=0;i<=n;i++) scanf("%f %f",&ax[i],&ay[i]); printf("nEnter the value of x for which the value of y is wanted: n"); scanf("%f",&x);
  • 7.
    h=ax[1]-ax[0]; for (i=0;i<=n-1;i++) diff[i][1] =ay[i+1]-ay[i]; for (j=2;j<=ORDER;j++); for(i=0;i<=n-j;i++) diff[i][j] = diff[i+1][j-1] - diff[i][j-1]; i=0; while (!(ax[i]>x)) i++; i--; p = (x-ax[i])/h; yp = ay[i]; for (k=1;k<=ORDER;k++) { nr *=p-k+1; dr *=k; yp +=(nr/dr)*diff[i][k]; } printf("nWhen x= %6.1f, corresponding y =%6.2fn",x,yp); } So, y(2.5)= 5.27
  • 8.
    Problem-3:Certain corresponding valuesof 𝑥 and log10𝑥 are (11,1.04139),(17,1.23044),(20,1.301029),(23,1.361727) Estimates the value of log1015 and find the error. Solution: packagedemo; import java.util.Scanner; public class DemoTranslation { public static void main(String[] args) { float[] x = new float[100], y = new float[100]; float a, s = 1, t = 1, k = 0; int n; boolean d = true; System.out.println("nEnter thenumber of the terms of the table: "); n = STDIN_SCANNER.nextInt(); System.out.println("nEnter therespectivevalues of the variables x and y: "); for(inti = 0; i < n; i++) { x[i] = STDIN_SCANNER.nextFloat(); y[i] = STDIN_SCANNER.nextFloat(); } System.out.println("nThetableyou entered is as follows :");
  • 9.
    for(inti = 0;i < n; i++) { System.out.printf("%.6ft%.6f", x[i], y[i]); System.out.println(); } while(d) { System.out.println(" nEnter the value of the x to find the respective value of y: "); a = STDIN_SCANNER.nextFloat(); for(inti = 0; i < n; i++) { s = 1; t = 1; for(intj = 0; j < n; j++) { if(j != i) { s = s * (a - x[j]); t = t * (x[i] - x[j]); } } k = k + (s / t) * y[i]; } System.out.printf("nTherespectivevalueof the variable y is: %fn", k); } } public final static Scanner STDIN_SCANNER=new Scanner(System.in);
  • 10.
  • 11.
    Problem-4:Using Newton divideddifference formula, find 𝑓(5) from the followingfunctional values 𝑓(3)=160,𝑓(9)=392,𝑓(13)=952,𝑓(18)=1300,𝑓(23)=1900 Solution: #include<stdio.h> #include<conio.h> void main() { int x[10], y[10], p[10]; int k,f,n,i,j=1,f1=1,f2=0; printf("nEnter the number of observations: n”); scanf("%d", &n); printf("nEnter the different values of x:n"); for (i=1;i<=n;i++) scanf("%d", &x[i]); printf("nThecorresponding values of y are:n"); for (i=1;i<=n;i++) scanf("%d", &y[i]); f=y[1]; printf("nEnter the value of 'k' in f(k) you want to evaluate: n");
  • 12.
    scanf("%d", &k); do { for (i=1;i<=n-1;i++) { p[i]= ((y[i+1]-y[i])/(x[i+j]-x[i])); y[i]=p[i]; } f1=1; for(i=1;i<=j;i++) { f1*=(k-x[i]); } f2+=(y[1]*f1); n--; j++; } while(n!=1); f+=f2; printf("nf(%d) =%d", k , f); getch(); }
  • 13.