-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumerical method problem solved
More file actions
1222 lines (962 loc) · 27.8 KB
/
Copy pathNumerical method problem solved
File metadata and controls
1222 lines (962 loc) · 27.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*//composite trapezoidal rule
#include<stdio.h>
#include<math.h>
float f(float x){
return 3*(x)*(x)+2*(x)-5;
}
//#define f(x) 3*(x)*(x)+2*(x)-5;
int main(void){
printf("sajan bista Trapezoidal composite rule\n ");
float x0,xn,fxn,fx0,h,step,a,v;
int i,k;
printf("enter the lower and upper bound\t");
scanf("%f%f",&x0,&xn);
printf("enter the number of segment");
scanf("%d",&k);
h=(xn-x0)/k;
fx0=f(x0);
fxn=f(xn);
step = f(x0)+ f(xn);
for(i=1;i<k;i++){
a=x0+i*h;
step= step +2*f(a);
}
v= h/2* step;
printf("Value of integration = %f\n",v);
return 0;
}
//simpson's 1/3 rule
//simpson's composite rule for 1/3
#include<stdio.h>
#include<math.h>
float f(float x){
return 3*(x)*(x)+2*(x)-5;
}
int main(void){
printf("sajan bista simpson's 1/3\n");
printf("enter the lower and upper limit \n");
float xn,x0,fx0,fxn,h,term,a,v;
int k,i;
scanf("%f%f",&x0,&xn);
printf("enter the number of segements\n");
scanf("%d",&k);
h=(xn-x0)/k;
fx0=f(x0);
fxn=f(xn);
term = f(x0)+f(xn);
for(i=1;i<=k-1;i+=2){
a= x0+i*h;
term = term+4*f(a);
}
for(i=1;i<=k-2;i+=2){
a= x0+i*h;
term = term+2*f(a);
}
v = h/3 * term;
printf("the output of the simpson's 1/3 rule %f ",v);
}
#include<stdio.h>
#include<math.h>
float f(float x){
return 3*(x)*(x)+2*(x)-5;
}
int main(void){
printf("sajan bista 3/8 simpson's rule\n");
float xn, x0,fxn,fx0,h,term,a,v;
int k,i;
printf("enter the upper and lower limit\n");
scanf("%f%f",&x0,&xn);
printf("enter the number of segment \n");
scanf("%d",&k);
h =(xn-x0)/k;
fx0 = f(x0);
fxn = f(xn);
term = f(x0)+f(xn);
for(i=1;i<=k-1;i++){
if(i%3!=0){
a= x0+i*h;
term = term+3*f(a);
}
else
{
a= x0+i*h;
term = term+2*f(a);
}
}
v= 3/8.0 *h*term;
printf("value of integration = %f",v);
}
// Gausssian quadrature
#include<stdio.h>
#include<math.h>
float f(float x){
return x*x*x+1;
}
int main(void){
printf("sajan bista\n Gaussian quadrature two-point\n ");
float c1,c2,z1,z2,b,a,x1,x2,v;
printf("Enter the lower and upper limit\n");
scanf("%f%f",&a,&b);
// setting the value of the parameters
c1=c2=1;
z1 =-0.557735;
z2=0.57735;
//calculating xi
x1=(b-a)/2*z1+(b+a)/2;
x2=(b-a)/2*z2+(b+a)/2;
//calculating integral value
v=(b-a)/2*((f(x1))+(f(x2)));
printf("value integration is =%f",v);
}
//Romberg estimate
#include<stdio.h>
#include<math.h>
float f(float x){
return x*x*x+1;
}
int main(void){
printf("sajan bista\n Romberg integration\n");
float x0,xn,T[10][10],h,sm,sl,a;
int i,k,c,r,m,p,q;
printf("Enter the lower and upper limit\n");
scanf("%f%f",&x0,&xn);
printf("Enter p & q of required T(p,q)\n");
scanf("%d%d",&p,&q);
h = xn-x0;
T[0][0] = h/2*((f(x0))+(f(xn)));
for(i=1;i<=p;i++){
sl = pow(2,i-1);
sm =0;
for(k=1;k<=sl;k++){
a =x0 +(2*k-1)*h/pow(2,i);
sm = sm+(f(a));
}
T[i][0]=T[i-1][0]/2+sm*h/pow(2,i);
}
for(c=1;c<=p;c++){
for(k=1;k<=c&&k<=q;k++){
m = c-k;
T[m+k][k]=(pow(4,k)*T[m+k][k-1]-T[m+k-1][k-1])/(pow(4,k)-1);
}
}
printf("Romberge Estimate of integration is = %f",T[p][q]);
}
//Taylor series
#include<stdio.h>
#include<math.h>
float fact(int n){
if(n==1)
return 1;
else{
return (n*fact(n-1));
}
}
int main(void){
printf("sajan bista\n Taylor series\n");
float x,x0,yx0,yx,fdy,sdy,tdy;
printf("Enter initial values of x & y \n");
scanf(" %f%f",&x0,&yx0);
printf("Enter x at which function to be evaluated \n");
scanf("%f",&x);
fdy=(x0)*(x0)+(yx0)*(yx0);//First Derivative
sdy= 2*(x0) + 2*(yx0)*fdy;// Second Derivative
tdy=2+2*yx0*sdy+2*fdy*fdy;// Third Derivative
yx=yx0+(x-x0)*fdy+(x-x0)*(x-x0)*sdy /fact(2)+(x-x0)*(x-x0)*(x-x0)*tdy /fact(3);
printf("Function value at x=%f is %f) n",x,yx);
}
//Euler's method
#include<stdio.h>
#include<math.h>
float f(float x,float y){
return 2*y/x;
}
int main(void){
printf("Sajan Bista\n");
float x, xp, x0,y0,y,h;
printf("Enter initial values of x & y \n");
scanf("%f%f", &x0,&y0);
printf("Enter x at which function to be Evaluated \n");
scanf("%f", &xp);
printf("Enter the step size\n ");
scanf("%f", &h);
y=y0;
x=x0;
for(x=x0;x<xp;x=x+h){
y=y+f(x,y)*h;
}
printf("Function value at x=%f is %f\n",xp,y);
}
#include<stdio.h>
#include<math.h>
float f(float x,float y) {
return 2*(y)/(x);
}
int main(void){
printf("sajan bista\n Heun's method\n");
float x, xp, x0, y0, y, h, m1, m2;
printf("Enter initial values of x & y \n");
scanf("%f%f",&x0,&y0);
printf("Enter x at which function to be Evaluated \n");
scanf("%f",&xp);
printf("Enter the step size\n");
scanf("%f",&h);
y = y0;
x = x0;
for(x=x0;x<xp;x=x+h){
m1=f(x,y);
m2=f(x+h,y+h*m1);
y=y+h/2*(m1+m2);
}
printf("Function value at x=%f is %f\n",xp,y);
}
#include<stdio.h>
#include<math.h>
float f(float x,float y) {
return 2*(x)+(y);
}
int main(void){
printf("sajan bista \n Fourth Order Runge-Kutta Method\n");
float x,xp,x0,y0,y,h,m1,m2,m3,m4;
printf("Enter initial values of x & y \n");
scanf("%f%f",&x0,&y0);
printf("Enter x at which function to be Evaluated\n");
scanf("%f",&xp);
printf("Enter the step size\n");
scanf("%f",&h);
y=y0;
x=x0;
for(x=x0;x<xp;x=x+h){
m1=f(x,y);
m2=f(x+1/2.0*h,y+1/2.0*h*m1);
m3=f(x+1/2.0*h,y +1/2.0*h*m2);
m4=f(x+h,y +h*m3);
y=y+h/6*(m1+2*m2+2*m3+m4);
}
printf("Function value at x=%f is %f\n",xp,y);
}
//shooting method
#include <stdio.h>
#include <math.h>
#define f1(x, y, z) (z) // Define the first function
#define f2(x, y, z) (6 * (x)) // Define the second function
int main(void)
{
printf("sajan bista\n");
float xa, xb, ya, yb, x, y, z, xp, h, sol, ny, nz, error, E, g[3], v[3], gs;
int i;
printf("Enter Boundary Conditions (xa, ya, xb, yb):\n");
scanf("%f %f %f %f", &xa, &ya, &xb, &yb);
printf("Enter x at which value is required:\n");
scanf("%f", &xp);
printf("Enter the step size:\n");
scanf("%f", &h);
printf("Enter accuracy limit:\n");
scanf("%f", &E);
x = xa;
y = ya;
g[1] = z = (yb - ya) / (xb - xa); // Initial slope guess
printf("Initial slope (g[1]) = %f\n", g[1]);
// First shooting iteration
while (x < xb) {
ny = y + (f1(x, y, z)) * h;
nz = z + (f2(x, y, z)) * h;
x += h;
y = ny;
z = nz;
if (fabs(x - xp) < 1e-6) { // Check if we reach xp
sol = y;
}
}
v[1] = y;
if (y < yb) {
g[2] = z = 2 * g[1];
} else {
g[2] = z = 0.5 * g[1];
}
printf("Updated slope guess (g[2]) = %f\n", g[2]);
// Second shooting iteration
x = xa;
y = ya;
z = g[2];
while (x < xb) {
ny = y + (f1(x, y, z)) * h;
nz = z + (f2(x, y, z)) * h;
x += h;
y = ny;
z = nz;
if (fabs(x - xp) < 1e-6) {
sol = y;
}
}
v[2] = y;
// Iterative correction
while (1) {
x = xa;
y = ya;
gs = g[2] - (v[2] - yb) / (v[2] - v[1]) * (g[2] - g[1]);
z = gs;
while (x < xb) {
ny = y + (f1(x, y, z)) * h;
nz = z + (f2(x, y, z)) * h;
x += h;
y = ny;
z = nz;
if (fabs(x - xp) < 1e-6) {
sol = y;
}
}
error = fabs(y - yb) / yb;
v[1] = v[2];
v[2] = y;
g[1] = g[2];
g[2] = gs;
if (error < E) {
printf("y(%f) = %f\n", xp,sol);
break;
}
}
return 0;
}
#include <stdio.h>
#include <math.h>
#define MAX 10 // Maximum dimension of the grid
#define EPSILON 1e-6 // Convergence criteria
int main(void){
printf("sajan bista\n");
int n, i, j, iteration = 0;
float tl, tr, tb, tu; // Temperatures on the boundaries
float grid[MAX][MAX], newGrid[MAX][MAX];
float error, maxError;
// Input the dimension of the plate
printf("Enter the number of grid points per side (n x n): ");
scanf("%d", &n);
if (n >= MAX) {
printf("Error: n exceeds maximum supported size (%d).\n", MAX - 1);
return 1;
}
// Input boundary conditions
printf("Enter the temperature at the left boundary: ");
scanf("%f", &tl);
printf("Enter the temperature at the right boundary: ");
scanf("%f", &tr);
printf("Enter the temperature at the bottom boundary: ");
scanf("%f", &tb);
printf("Enter the temperature at the top boundary: ");
scanf("%f", &tu);
// Initialize the grid with boundary conditions
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == 0) // Bottom boundary
grid[i][j] = tb;
else if (i == n - 1) // Top boundary
grid[i][j] = tu;
else if (j == 0) // Left boundary
grid[i][j] = tl;
else if (j == n - 1) // Right boundary
grid[i][j] = tr;
else // Interior points
grid[i][j] = 0.0;
}
}
// Iteratively solve using Gauss-Seidel method
do {
maxError = 0.0;
for (i = 1; i < n - 1; i++) {
for (j = 1; j < n - 1; j++) {
// Update the grid using the finite difference formula
newGrid[i][j] = 0.25 * (grid[i-1][j] + grid[i+1][j] +
grid[i][j-1] + grid[i][j+1]);
// Compute the error
error = fabs(newGrid[i][j] - grid[i][j]);
if (error > maxError)
maxError = error;
// Update the grid in place
grid[i][j] = newGrid[i][j];
}
}
iteration++;
} while (maxError > EPSILON);
// Output the result
printf("\nSolution converged in %d iterations.\n", iteration);
printf("Temperature distribution on the plate:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%8.4f ", grid[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <math.h>
#define MAX 50 // Maximum grid size
#define EPSILON 1e-6 // Convergence criterion
int main() {
printf("sajan bista\n");
int n, i, j, iter = 0;
double h, x[MAX][MAX], f[MAX][MAX], error, maxError;
// Input grid size and initialize
printf("Enter grid size (n x n): ");
scanf("%d", &n);
if (n >= MAX) {
printf("Grid size too large! Max is %d.\n", MAX - 1);
return 1;
}
printf("Enter grid spacing (h): ");
scanf("%lf", &h);
// Initialize source term f(x, y) and grid x[i][j]
printf("Enter the source term values f(x, y):\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("f[%d][%d]: ", i, j);
scanf("%lf", &f[i][j]);
x[i][j] = 0.0; // Initial guess
}
}
// Iterative Gauss-Seidel method
do {
maxError = 0.0;
for (i = 1; i < n - 1; i++) {
for (j = 1; j < n - 1; j++) {
double oldVal = x[i][j];
x[i][j] = 0.25 * (x[i - 1][j] + x[i + 1][j] +
x[i][j - 1] + x[i][j + 1] -
h * h * f[i][j]);
error = fabs(x[i][j] - oldVal);
if (error > maxError)
maxError = error;
}
}
iter++;
} while (maxError > EPSILON);
// Output solution
printf("\nSolution converged in %d iterations.\n", iter);
printf("Solution:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%8.4f ", x[i][j]);
}
printf("\n");
}
return 0;
}
//linear regression
#include <stdio.h>
int main() {
printf("sajan bista\n");
int n, i;
float a = 0, b = 0, x[10], y[10];
float sx = 0, sy = 0, sxy = 0, sx2 = 0;
// Input the number of data points
printf("Enter the number of points n: ");
scanf("%d", &n);
// Input the x and y values
printf("Enter the values of x and y:\n");
for (i = 0; i < n; i++) {
printf("x[%d], y[%d]: ", i, i);
scanf("%f %f", &x[i], &y[i]);
}
// Calculate summations
for (i = 0; i < n; i++) {
sx += x[i];
sy += y[i];
sxy += x[i] * y[i];
sx2 += x[i] * x[i];
}
// Compute the slope (b) and intercept (a)
b = ((n * sxy) - (sx * sy)) / ((n * sx2) - (sx * sx));
a = (sy / n) - (b * (sx / n));
// Display the fitted line equation
printf("\nFitted line equation: y = %.2f + %.2f * x\n", a, b);
return 0;
}
//exponential
#include <stdio.h>
#include <math.h> // For log() and exp()
int main() {
printf("sajan bista\n");
int n, i;
float x[10], y[10], log_y[10];
float sx = 0, sy = 0, sxy = 0, sx2 = 0;
float a, b, A;
// Input the number of data points
printf("Enter the number of points n: ");
scanf("%d", &n);
// Input the x and y values
printf("Enter the values of x and y:\n");
for (i = 0; i < n; i++) {
printf("x[%d], y[%d]: ", i, i);
scanf("%f %f", &x[i], &y[i]);
if (y[i] <= 0) {
printf("Error: y values must be positive for exponential regression.\n");
return 1;
}
log_y[i] = log(y[i]); // Compute ln(y)
}
// Calculate summations
for (i = 0; i < n; i++) {
sx += x[i];
sy += log_y[i];
sxy += x[i] * log_y[i];
sx2 += x[i] * x[i];
}
// Compute b and A
b = ((n * sxy) - (sx * sy)) / ((n * sx2) - (sx * sx));
A = (sy / n) - (b * (sx / n));
// Compute a = exp(A)
a = exp(A);
// Display the fitted exponential equation
printf("\nFitted exponential equation: y = %.2f * e^(%.2f * x)\n", a, b);
return 0;
}
//polynomial regression
#include <stdio.h>
#include <math.h>
#define MAX 10 // Maximum number of data points
#define DEG 5 // Maximum degree of the polynomial
int main() {
printf("sajan bista\n");
int n, degree, i, j, k;
double x[MAX], y[MAX], X[2 * DEG + 1], B[DEG + 1], A[DEG + 1][DEG + 2], coeff[DEG + 1];
// Input the number of data points
printf("Enter the number of data points (n): ");
scanf("%d", &n);
// Input the degree of the polynomial
printf("Enter the degree of the polynomial: ");
scanf("%d", °ree);
// Input x and y values
printf("Enter the values of x and y:\n");
for (i = 0; i < n; i++) {
printf("x[%d], y[%d]: ", i, i);
scanf("%lf %lf", &x[i], &y[i]);
}
// Initialize summations for X and B
for (i = 0; i <= 2 * degree; i++) {
X[i] = 0;
for (j = 0; j < n; j++) {
X[i] += pow(x[j], i);
}
}
for (i = 0; i <= degree; i++) {
B[i] = 0;
for (j = 0; j < n; j++) {
B[i] += pow(x[j], i) * y[j];
}
}
// Construct the augmented matrix
for (i = 0; i <= degree; i++) {
for (j = 0; j <= degree; j++) {
A[i][j] = X[i + j];
}
A[i][degree + 1] = B[i];
}
// Perform Gaussian elimination
for (i = 0; i <= degree; i++) {
for (j = 0; j <= degree; j++) {
if (j != i) {
double ratio = A[j][i] / A[i][i];
for (k = 0; k <= degree + 1; k++) {
A[j][k] -= ratio * A[i][k];
}
}
}
}
// Extract coefficients
for (i = 0; i <= degree; i++) {
coeff[i] = A[i][degree + 1] / A[i][i];
}
// Display the polynomial equation
printf("\nThe fitted polynomial is:\n");
printf("y = ");
for (i = 0; i <= degree; i++) {
if (i == 0) {
printf("%.4lf", coeff[i]);
} else {
printf(" + %.4lf*x^%d", coeff[i], i);
}
}
printf("\n");
return 0;
}
//gauss elimination method
#include <stdio.h>
#include <math.h>
#define MAX 10 // Maximum number of variables
void gaussElimination(float a[MAX][MAX], int n) {
int i, j, k;
float factor, sum, x[MAX];
// Forward Elimination
for (k = 0; k < n - 1; k++) {
for (i = k + 1; i < n; i++) {
factor = a[i][k] / a[k][k];
for (j = k; j <= n; j++) {
a[i][j] -= factor * a[k][j];
}
}
}
// Back Substitution
x[n - 1] = a[n - 1][n] / a[n - 1][n - 1];
for (i = n - 2; i >= 0; i--) {
sum = 0;
for (j = i + 1; j < n; j++) {
sum += a[i][j] * x[j];
}
x[i] = (a[i][n] - sum) / a[i][i];
}
// Display the solution
printf("\nThe solution is:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %.4f\n", i + 1, x[i]);
}
}
int main() {
printf("sajan bista\n");
int n, i, j;
float a[MAX][MAX];
// Input number of variables
printf("Enter the number of variables: ");
scanf("%d", &n);
// Input augmented matrix
printf("Enter the augmented matrix (coefficients and constants):\n");
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
printf("a[%d][%d]: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
}
// Perform Gauss Elimination
gaussElimination(a, n);
return 0;
}
#include <stdio.h>
#include <math.h>
#define MAX 10 // Maximum number of variables
void gaussJordan(float a[MAX][MAX], int n) {
int i, j, k;
float factor;
// Convert matrix to reduced row-echelon form
for (i = 0; i < n; i++) {
// Make the diagonal element 1
factor = a[i][i];
for (j = 0; j <= n; j++) {
a[i][j] /= factor;
}
// Make all other elements in the column 0
for (k = 0; k < n; k++) {
if (k != i) {
factor = a[k][i];
for (j = 0; j <= n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
}
// Display the solution
printf("\nThe solution is:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %.4f\n", i + 1, a[i][n]);
}
}
int main() {
printf("sajan bista\n");
int n, i, j;
float a[MAX][MAX];
// Input number of variables
printf("Enter the number of variables: ");
scanf("%d", &n);
// Input augmented matrix
printf("Enter the augmented matrix (coefficients and constants):\n");
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
printf("a[%d][%d]: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
}
// Perform Gauss-Jordan Elimination
gaussJordan(a, n);
return 0;
}
//
#include <stdio.h>
#include <math.h>
#define MAX 10 // Maximum size of the matrix
void gaussJordanInverse(float a[MAX][MAX], float inverse[MAX][MAX], int n) {
int i, j, k;
float factor;
// Augmenting the matrix with the identity matrix
float augmented[MAX][2*MAX];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
augmented[i][j] = a[i][j];
augmented[i][j + n] = (i == j) ? 1 : 0; // Identity matrix
}
}
// Perform Gauss-Jordan elimination
for (i = 0; i < n; i++) {
// Make the diagonal element 1
factor = augmented[i][i];
for (j = 0; j < 2*n; j++) {
augmented[i][j] /= factor;
}
// Make the rest of the column elements 0
for (k = 0; k < n; k++) {
if (k != i) {
factor = augmented[k][i];
for (j = 0; j < 2*n; j++) {
augmented[k][j] -= factor * augmented[i][j];
}
}
}
}
// Extract the inverse matrix from the augmented matrix
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
inverse[i][j] = augmented[i][j + n];
}
}
}
int main() {
printf("sajan bista\n");
int n, i, j;
float a[MAX][MAX], inverse[MAX][MAX];
// Input number of variables (order of the matrix)
printf("Enter the order of the matrix: ");
scanf("%d", &n);
// Input matrix
printf("Enter the elements of the matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("a[%d][%d]: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
}
// Call Gauss-Jordan method to compute the inverse
gaussJordanInverse(a, inverse, n);
// Output the inverse matrix
printf("\nThe inverse of the matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%.4f ", inverse[i][j]);
}
printf("\n");
}
return 0;
}
//matrix factorization method
#include <stdio.h>
#include <stdlib.h>
#define MAX 10 // Maximum size of matrix
// Function to perform LU Decomposition
void luDecomposition(float mat[MAX][MAX], float L[MAX][MAX], float U[MAX][MAX], int n) {
int i, j, k;
// Initialize L and U matrices to zero
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
L[i][j] = 0;
U[i][j] = 0;
}
}
// LU Decomposition
for (i = 0; i < n; i++) {
// Upper Triangular Matrix
for (j = i; j < n; j++) {
U[i][j] = mat[i][j];
for (k = 0; k < i; k++) {
U[i][j] -= L[i][k] * U[k][j];
}
}
// Lower Triangular Matrix
for (j = i; j < n; j++) {
if (i == j) {
L[i][i] = 1; // Diagonal elements of L are set to 1
} else {
L[j][i] = mat[j][i];
for (k = 0; k < i; k++) {
L[j][i] -= L[j][k] * U[k][i];
}
L[j][i] /= U[i][i];
}
}
}
}
// Function to print a matrix
void printMatrix(float mat[MAX][MAX], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {