0% found this document useful (0 votes)
24 views1 page

C Programs (SEM-2)

C programming for msc

Uploaded by

satyajeet32422
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)
24 views1 page

C Programs (SEM-2)

C programming for msc

Uploaded by

satyajeet32422
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

C Programs

1. Romberg Integration of a given function using Trapezoidal Rule

#include<stdio.h>
#include<math.h>

void main() {
float a,b,h,error=1,eps=1e-5,I[10][10];
int n=1,j=0,k,l,maxrow;
float trap(float a, float h, int n);
float f(float x);

printf("Enter the limits a and b: ");


scanf("%f %f",&a,&b);

printf("Enter max. number of rows to be computed: ");


scanf("%d",&maxrow);

h=b-a;
I[0][0]=h*(f(a)+f(b))/2;

printf("Romberg integration table\n\n");


while((error>eps) && (j<maxrow)) {
j++; h/=2; n*=2;
I[j][0]=trap(a,h,n);

for(k=1;k<=j;k++)
I[j][k]=I[j][k-1]+(I[j][k-1]-I[j-1][k-1])/(pow(4,k)-1);

for(l=0;l<=j;l++)
printf("\t%f\t",I[j][l]);

printf("\n");
error=fabs(I[j-1][j-1]-I[j][j]);
}
if(error<eps)
printf("The value of the integration is %f ",I[j][j]);
else {
printf("The result does not achieve the desired accuracy");
printf("\nthe best approximation is %f ",I[j][j]);
}
}

float f(float x){


return(x*exp(x));
}

float trap(float a, float h, int n){


float sum=0;
int i;
for(i=1;i<=n-1;i++) sum+=f(a+i*h);
sum=(f(a)+f(a+n*h)+2*sum)*h/2;
return(sum);
}

2. Gauss Seidal Method

#include<stdio.h>
#include<math.h>

void main(){
float a[10][10], b[10], x[10], xn[10], eps=0.000001, sum;
int i, j, n, flag;

printf("Enter the number of variables: ");


scanf("%d", &n);

printf("Enter the coefficient row wise:\n");


for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%f", &a[i][j]);

printf("Enter the right hand vector\n");


for(i=1; i<=n; i++)
scanf("%f", &b[i]);

// setting initial solution as 0


for(i=0; i<=n; i++)
x[i] = 0;

do{
for(i=1; i<=n; i++){
sum = b[i];
for(j=1; j<=n; j++){
if(j<i)
sum -= a[i][j]*xn[j];
else if(j>i)
sum -= a[i][j]*x[j];

xn[i] = sum/a[i][i];
}
}
flag=0;
for(i=1; i<=n; i++)
if(fabs(x[i]-xn[i]) > eps)
flag = 1;

if(flag == 1)
for(i=0; i<=n; i++)
x[i] = xn[i];

} while(flag == 1);

printf("solution is:\n");
for(i=1; i<=n; i++)
printf("%f ", xn[i]);

3. Runge Kutta Method

#include<stdio.h>
#include<math.h>

void main(){
float x0,y0,z0,xn,h,x,y,z,k1,k2,k3,k4,n1,n2,n3,n4;
float f(float x, float y, float z);
float g(float x, float y, float z);

printf("Enter the initial value of x,y and z: ");


scanf("%f%f%f",&x0,&y0,&z0);

printf("Enter the last value of x: ");


scanf("%f",&xn);

printf("Enter the step length: ");


scanf("%f",&h);

y=y0;
z=z0;
printf("x value \t y value \t z value\n");

for(x=x0;x<xn;x+=h){
k1=h*f(x,y,z);
n1=h*g(x,y,z);
k2=h*f(x+h/2, y+k1/2,z+n1/2);
n2=h*g(x+h/2,y+k1/2,z+n1/2);
k3=h*f(x+h/2,y+k2/2,z+n2/2);
n3=h*g(x+h/2,y+k2/2,z+n2/2);
k4=h*f(x+h,y+k3,z+n3);
n4=h*g(x+h,y+k3,z+n3);
y=y+(k1+2*(k2+k3)+k4)/6;
z=z+(n1+2*(n2+n3)+n4)/6;
printf("%f\t%f\t%f\n",x+h,y,z);
}
}

float f(float x, float y, float z){


return(z);
}

float g(float x, float y, float z){


return (0.5*(45*exp(2*x) + 5*z + 3*y));
}

4. Parabolic PDE

#include <stdio.h>
#include <math.h>
#define x 8
#define t 5

float fun(int a){


return (4*a - (a*a) / 2);
}

void main(){
float u[x+1][t+1], h = 1.0, k = 0.125, c, al, us, ue;
int i, j;

printf("enter the square value of c: ");


scanf("%f", &c);
al = c * k / pow(h, 2);

printf("enter the value of u(0,t): ");


scanf("%f", &us);

printf("enter the value of u(%d,t): ", x);


scanf("%f", &ue);

for(j=0; j <= t; j++){


u[0][j] = us;
u[x][j] = ue;
}
for(i = 0; i <= x - 1; i++) {
u[i][0] = fun(i);
}

for(j = 0; j <= t - 1; j++) {


for(i = 1; i <= x - 1; i++) {
u[i][j+1] = al * u[i-1][j] + (1-2*al) * u[i][j] + al * u[i+1]
[j];
}
}

printf("the value of u(i,j) are:");

for(j = 0; j < t; j++) {


for(i = 0; i < x; i++) {
if(i % 8 == 0)
printf("\n");
printf("%6.4f\t", u[i][j]);
}
}
}

5. Matrix inverse using Gauss-Jordan Method

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define zero 0.00001

void main(){
int n, m, k, i, j;
float a[10][20], temp;

printf("\n Enter the size of the matrix: ");


scanf("%d", &n);

printf("Enter elements row wise\n");


for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
scanf("%f", &a[i][j]);

for(i = 1; i <= n; i++)


for(j = 1; j <= n; j++)
a[i][n+j] = 0;

for(i = 1; i <= n; i++)


a[i][n+i] = 1;

m = 2*n;
for(k = 1; k <= n; k++){
temp = fabs(a[k][k]);
j = k;
for(i = k+1; i <= n; i++)
if(temp < fabs(a[i][k])){
temp = fabs(a[i][k]);
j = i;
}
if(fabs(a[j][k]) <= zero){
printf("The matrix is singular and is not invertible");
exit(0);
}
if(j != k){
for(i = 1; i <= m; i++){
temp = a[j][i];
a[j][i] = a[k][i];
a[k][i] = temp;
}
}
if(a[k][k] != 1){
temp = a[k][k];
for(i = 1; i <= m; i++)
a[k][i] /= temp;
}
for(j = k+1; j <= n; j++){
temp = a[j][k];
for(i = 1; i <= m; i++)
a[j][i] -= temp * a[k][i];
}
}
for(k = 2; k <= n; k++){
for(j = k-1; j >= 1; j--){
temp = a[j][k];
for(i = 1; i <= m; i++)
a[j][i] -= temp * a[k][i];
}
}
printf("\nThe inverse matrix is \n");
for(i = 1; i <= n; i++){
for(j = n+1; j <= m; j++){
printf("%f\t", a[i][j]);
}
printf("\n");
}
}

6. Hyperbolic PDE

#include <stdio.h>
#include <math.h>
#define x 5
#define T 8

void main(){
float u[x+1][T+1], c, ut, ue, fun(float);
int i, j;

printf("Enter the value of c: ");


scanf("%f", &c);

printf("Enter the value of ut and ue: ");


scanf("%f%f", &ut, &ue);

for(j = 0; j <= T; j++){


u[0][j] = ut;
u[x][j] = ue;
}

for(i = 1; i <= x-1; i++)


u[i][0] = fun(3.14*i*0.2);

for(i = 1; i <= x-1; i++)


u[i][1] = 0.125 * (u[i-1][0] + u[i+1][0]) + 0.75*u[i][0];

for(j = 1; j <= T-1; j++)


for(i = 1; i <= x-1; i++)
u[i][j+1] = c*c*u[i-1][j] + c*c*u[i+1][j] + 2*(1-c*c)*u[i][j] -
u[i][j-1];

printf("the value of u[i][j] are: \n");


for(j = 0; j <= T-3; j++){
for(i = 0; i <= x; i++){
printf("%10.4f\t", u[i][j]);
}
printf("\n");
}
}

float fun(float y){


return (0.5 * sin(y));
}

7. Elliptic type PDE

#include <stdio.h>
#include <math.h>

void main(){
int i, j, n;
float u[6][6], un[6][6], h, k;
float g(float x, float y);
float f1(float x);
float f2(float y);
float f3(float x);
float f4(float y);

printf(" Enter the sub intervals n: ");


scanf("%d", &n);

printf(" Enter the step lengths h and k: ");


scanf("%f%f", &h, &k);

// set the boundary conditions


for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
u[i][j] = 0;

for(i = 0; i <= n; i++){


u[i][0] = f1(i*h);
u[i][n] = f3(i*h);
}

for(j = 0; j <= n; j++){


u[0][j] = f2(j*k);
u[n][j] = f4(j*k);
}

for(i = 0; i <= n; i++)


for(j = 0; j <= n; j++)
un[i][j] = u[i][j];

for(i = 1; i < n; i++)


for(j = 1; j <= n; j++)
un[i][j] = u[i][j];

for(i = 1; i < n; i++)


for(j = 1; j < n; j++)
un[i][j] = 0.25*((un[i-1][j] + u[i+1][j] + un[i][j-1] +
u[i][j+1])- h*h*g(i*h, j*k));

printf("The solution at different points are \n");


for(i = 0; i <= n; i++){
printf("%8.5f", i*h);
}

printf("\n-----------------------\n");

for(i = 0; i <= n; i++){


printf("%8.5f|", i * k);
for(j = 0; j <= n; j++){
printf("%8.5f", un[i][j]);
}
printf("\n");
}
}

float g(float x, float y){


return (-2*x*x + y*y);
}
float f1(float x){
return (0);
}
float f2(float y){
return (0);
}
float f3(float x){
return (0);
}
float f4(float y){
return (0);
}

8. Milne’s method

#include <stdio.h>
#include <math.h>

void main(){
float x0, y0, xn, h, y1, y2, y3, yc, yp;
float eps = 1e-5;
float x1, x2, x3, x4, f0, f1, f2, f3, yold;

float f(float x, float y);


float rk4(float x, float y, float h);

printf("Enter the initial value of x and y: ");


scanf("%f %f", &x0, &y0);
printf("Enter the last value of x: ");
scanf("%f", &xn);

printf("Enter the step length h: ");


scanf("%f", &h);

printf("x- value y-value \n");


x1 = x0 + h;
x2 = x1 + h;
x3 = x2 + h;
y1 = rk4(x0, y0, h);
y2 = rk4(x1, y1, h);
y3 = rk4(x2, y2, h);
f1 = f(x1, y1);
f2 = f(x2, y2);
f3 = f(x3, y3);
for(x4 = x3+h; x4 <= xn; x4 += h){
yp = y0 + 4*h*(2*f1-f2 + 2*f3) / 3;
yold = yp;
yc = yp;
do{
yold = yc;
yc = y2 + h * (f2 + 4*f3 + f(x4, yold)) / 3;
} while((yc - yold) > eps);

printf("%8.4f %8.5f\n", x4, yc);


y0 = y1;
y1 = y2;
y2 = y3;
y3 = yc;
f1 = f2;
f2 = f3;
f3 = f(x4, yc);
}
}

float f(float x, float y){


return (2*x + y*y);
}

float rk4(float x, float y, float h)


{
float k1, k2, k3, k4;
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);
y = y + (k1 + 2 * (k2 + k3) + k4) / 6;
return (y);
}

You might also like