You are on page 1of 2

Untitled

1 /*NAME: SUBHAJIT BAKSHI


2 ROLL:100173320250028
3 DATE:12/12/18
4
5 QUESTION:
6 write a c program to solve order BVP y"+p(x)y'+q(x)y=r(x)
7 with y(x0)=y0,y(xn)=yn using finite difference method
8
9 ANSWER:
10 c program:finite difference method to solve the second order BVP
11 y"+p(x)y'+q(x)y=r(x) with y(x0)=y0,y(xn)=yn. here we consider the
12 equation y"+2xy'+y=5x with boundary conditions y(0)=0 and y(1)=0 */
13
14 #include <stdio.h>
15 #include <math.h>
16 #include<stdlib.h>
17 float y[10];
18 void main()
19 {
20 int i,n;
21 float a[10],b[10],c[10],d[10],x0,xn,y0,yn,temp,h,x;
22 float p(float x);float q(float x);float r(float x);
23 float TriDiag(float a[],float b[],float c[],float d[],int n);
24 printf("enter the initial and final values of x \n");
25 scanf("%f %f",&x0,&xn);
26 printf("enter the initial and final values of y\n");
27 scanf("%f %f",&y0,&yn);
28 printf("enter the number of sub intervals\n");
29 scanf("%d",&n);
30 h=(xn-x0)/n;
31 x=x0;
32 for(i=1;i<=n-1;i++)
33 {
34 x+=h;
35 a[i]=2*h*h*q(x)-4;
36 b[i]=2+h*p(x);
37 c[i]=2-h*p(x);
38 d[i]=2*h*h*r(x);
39 }
40 /*end of loop i*/
41 d[1]-=c[1]*y0;
42 d[n-1]-=b[n-1]*yn;
43 temp=TriDiag(c,a,b,d,n-1);
44 y[0]=y0; y[n]=yn;
45 printf("the solution is\n x-value y-value\n");
46 for(i=0;i<=n;i++) printf("%8.5f %8.5f\n",x0+i*h,y[i]);
47 }/*main*/
48 /* definition of the functions p(x),q(x) and r(x)*/
49 float p(float x)
50 {
51 return(2*x);
52 }
53 float q(float x)
54 {
55 return(1);
56 }
57 float r(float x)
58 {
59 return(5*x);
Page 1
Untitled
60 }
61 float TriDiag(float a[10],float b[10],float c[10],float d[10],int n)
62 {
63 /*output y[i],i=1,2,.....,n is a global variable*/
64 int i;float gamma[10],z[10];
65 gamma[1]=b[1];
66 for(i=2;i<=n;i++)
67 {
68 if(gamma[i-1]==0.0)
69 {
70 printf("a minor is zero;method fails");
71 exit(0);
72 }
73 gamma[i]=b[i]-a[i]*c[i-1]/gamma[i-1];
74 }
75 z[1]=d[1]/gamma[1];
76 for(i=2;i<=n;i++)
77 z[i]=(d[i]-a[i]*z[i-1])/gamma[i];
78 y[n]=z[n];
79 for(i=n-1;i>=1;i--)
80 y[i]=z[i]-c[i]*y[i+1]/gamma[i];
81 return(y[0]);
82 }/*end of tridiag*/
83 /*
84 *******************SAMPLE INPUT/OUTPUT***********
85 enter the initial and final values of x
86 0 1
87 enter the initial and final values of y
88 0 0
89 enter the number of sub intervals
90 4
91 the solution is
92 x-value y-value
93 0.00000 0.00000
94 0.25000 -0.22256
95 0.50000 -0.33231
96 0.75000 -0.26032
97 1.00000 0.00000
98
99 Process returned 4 (0x4) execution time : 25.228 s
100 Press any key to continue.
101
102

Page 2

You might also like