Unit 4 & 5
Unit 4 & 5
Unit 4 & 5
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
printf("Enter 3*3 matrix 1 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d",&mat1[i][j]);
}
printf("Enter 3*3 matrix 2 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d",&mat2[i][j]);
}
printf("\nAdding the two matrix.....");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
printf("\nBoth matrix added successfully!");
printf("\nHere is the new matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d ",mat3[i][j]);
printf("\n");
}
getch();
return 0;
}
Out Put:-
02) Subtraction of two Matrix
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], matSub[3][3], i, j;
printf("Enter First 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat1[i][j]);
}
printf("Enter Second 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat2[i][j]);
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matSub[i][j] = mat1[i][j] - mat2[i][j];
}
printf("\nThe Subtraction Result is:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d ", matSub[i][j]);
printf("\n");
}
getch();
return 0;
}
Out Put:-
03) Multiplication of two Matrix
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
printf("Enter first 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat1[i][j]);
}
printf("Enter second 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat2[i][j]);
}
printf("\nMultiplying two matrices...");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
sum = sum + mat1[i][k] * mat2[k][j];
mat3[i][j] = sum;
}
}
printf("\nMultiplication result of the two given Matrix is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d\t", mat3[i][j]);
printf("\n");
}
getch();
return 0;
}
Out Put:-
04) Transpose of Matrix
#include<stdio.h>
#include<conio.h>
int main()
{
int mat[3][3], i, j, matTrans[3][3];
printf("Enter 3*3 Matrix Elements: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat[i][j]);
}
// Transposing the Matrix...
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matTrans[j][i] = mat[i][j];
}
printf("\nTranspose of given Matrix is:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d ", matTrans[i][j]);
printf("\n");
}
getch();
return 0;
}
Out Put:-
Fourth order, Runge - Kutta method for solution of first order:-
Example 1:- Consider an ordinary differential equation dy/dx = x2 + y2, y(1) = 1.2. Find y(1.05) using
the fourth order Runge-Kutta method.
Solution:
Given,
dy/dx = x2 + y2, y(1) = 1.2
So, f(x, y) = x2 + y2
x0 = 1 and y0 = 1.2
Also, h = 0.05
Let us calculate the values of k1, k2, k3 and k4.
k1 = hf(x0, y0)
= (0.05) [x02 + y02]
= (0.05) [(1)2 + (1.2)2]
= (0.05) (1 + 1.44)
= (0.05)(2.44)
= 0.122
k2 = hf[x0 + (½)h, y0 + (½)k1]
= (0.05) [f(1 + 0.025, 1.2 + 0.061)] {since h/2 = 0.05/2 = 0.025 and k1/2 = 0.122/2 = 0.061}
= (0.05) [f(1.025, 1.261)]
= (0.05) [(1.025)2 + (1.261)2]
= (0.05) (1.051 + 1.590)
= (0.05)(2.641)
= 0.1320
k3 = hf[x0 + (½)h, y0 + (½)k2]
= (0.05) [f(1 + 0.025, 1.2 + 0.066)] {since h/2 = 0.05/2 = 0.025 and k2/2 = 0.132/2 = 0.066}
= (0.05) [f(1.025, 1.266)]
= (0.05) [(1.025)2 + (1.266)2]
= (0.05) (1.051 + 1.602)
= (0.05)(2.653)
= 0.1326
k4 = hf(x0 + h, y0 + k3)
= (0.05) [f(1 + 0.05, 1.2 + 0.1326)]
= (0.05) [f(1.05, 1.3326)]
= (0.05) [(1.05)2 + (1.3326)2]
= (0.05) (1.1025 + 1.7758)
= (0.05)(2.8783)
= 0.1439
By RK4 method, we have;
y1 = y0 + (⅙) (k1 + 2k2 + 2k3 + k4)
y1 = y(1.05) = y0 + (⅙) (k1 + 2k2 + 2k3 + k4)
By substituting the values of y0, k1, k2, k3 and k4, we get;
y(1.05) = 1.2 + (⅙) [0.122 + 2(0.1320) + 2(0.1326) + 0.1439]
= 1.2 + (⅙) (0.122 + 0.264 + 0.2652 + 0.1439)
= 1.2 + (⅙) (0.7951)
= 1.2 + 0.1325
= 1.3325
Example 2:- Find the value of k1 by Runge-Kutta method of fourth order if dy/dx = 2x + 3y2 and
y(0.1) = 1.1165, h = 0.1.
Solution:
Given,
dy/dx = 2x + 3y2 and y(0.1) = 1.1165, h = 0.1
So, f(x, y) = 2x + 3y2
x0 = 0.1, y0 = 1.1165
By Runge-Kutta method of fourth order, we have
k1 = hf(x0, y0)
= (0.1) f(0.1, 1.1165)
= (0.1) [2(0.1) + 3(1.1165)2]
= (0.1) [0.2 + 3(1.2465)]
= (0.1)(0.2 + 3.7395)
= (0.1)(3.9395)
= 0.39395
Runge - Kutta method for solution of second order differential equations:-
Program :-
#include <stdio.h>
return y;
}
// Driver Code
int main()
{
float x0 = 0, y = 1,
x = 2, h = 0.2;
printf("y(x) = %f",
rungeKutta(x0, y, x, h));
return 0;
}
Out Put:-
y(x) = 0.645590
Unit -5
Algorithm:-
Start
Read a, b, c values
Compute d = b2 4ac
if d > 0 then
o r1 = b+ sqrt (d)/(2*a)
o r2 = b sqrt(d)/(2*a)
Otherwise if d = 0 then
o compute r1 = -b/2a, r2=-b/2a
o print r1,r2 values
Otherwise if d < 0 then print roots are imaginary
Stop
Program:-
# include<stdio.h>
# include<conio.h>
# include<math.h>
main (){
float a,b,c,r1,r2,d;
printf (“enter the values of a b c”);
scanf (“ %f %f %f”, &a, &b, &c);
d= b*b – 4*a*c;
if (d>0){
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf (“The real roots = %f %f”, r1, r2);
}
else if (d= =0){
r1 = -b/(2*a);
r2 = -b/(2*a);
printf (“roots are equal =%f %f”, r1, r2);
}
else
printf(“Roots are imaginary”);
getch ();
}
Out Put:-