0% found this document useful (0 votes)
11 views

Unit 4 & 5

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)
11 views

Unit 4 & 5

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
You are on page 1/ 17

Unit – 4

01) Addition of two Matrix

#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 :-

// C program to implement Runge


// Kutta method

#include <stdio.h>

// A sample differential equation


// "dy/dx = (x - y)/2"
float dydx(float x, float y)
{
return (x + y - 2);
}

// Finds value of y for a given x


// using step size h
// and initial value y0 at x0.
float rungeKutta(float x0, float y0,
float x, float h)
{
// Count number of iterations
// using step size or
// step height h
int n = (int)((x - x0) / h);

float k1, k2;

// Iterate for number of iterations


float y = y0;
for (int i = 1; i <= n; i++) {
// Apply Runge Kutta Formulas
// to find next value of y
k1 = h * dydx(x0, y);
k2 = h * dydx(x0 + 0.5 * h,
y + 0.5 * k1);

// Update next value of y


y = y + (1.0 / 6.0) * (k1 + 2 * k2);

// Update next value of x


x0 = x0 + 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

Find roots of a quadratic equation, ax2+bx+c.

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:-

Case 1: enter the values of a b c: 1 4 3


r1 = -1
r2 = -3
Case 2: enter the values of a b c: 1 2 1
r1 = -1
r2 = -1
Case 3: enter the values of a b c: 1 1 4
Roots are imaginary
Numerical integral using Trapezoidal Rule:-
Finding root of equation f (x) = 0 by Newton –Raphson:-
Example 1:
Find the cube root of 12 using the Newton Raphson method assuming x0 = 2.5.
Solution:
We know that, the iterative formula to find bth root of a is given by:
xn+1=1b[(b−1)xn+axnb−1]
From the given, a = 12, b = 3
Let x0 be the approximate cube root of 12, i.e., x0 = 2.5.
So, x1 = (⅓) [2x0 + 12/x02]
= (⅓) [2(2.5) + 12/(2.5)2]
= (⅓) [5 + 12/6.25]
= (⅓)(5 + 1.92)
= 6.92/3
= 2.306
Now,
x2 = (⅓)[2x1 + 12/x12]
= (1/3) [2(2.306) + 12/(2.306)2]
= (⅓) [4.612 + 12/5.3176]
= (⅓) [4.612 + 2.256]
= 6.868/3
= 2.289
Therefore, the approximate cube root of 12 is 2.289.
Example 2:
Find a real root of the equation -4x + cos x + 2 = 0, by Newton Raphson method up to four decimal
places, assuming x0 = 0.5.
Solution:
Given equation: -4x + cos x + 2 = 0
x0 = 0/5
Let f(x) = -4x + cos x + 2
f’(x) = -4 – sin x
Now,
f(0) = -4(0) + cos 0 + 2 = 1 + 2 = 3 > 0
f(1) = -4(1) + cos 1 + 2 = -4 + 0.5403 + 2 = -1.4597 < 0
Thus, a root lies between 0 and 1.
Let us find the first approximation.
x1 = x0 – f(x0)/f’(x0)
= 0.5 – [-4(0.5) + cos 0.5 + 2]/ [-4 – sin 0.5]
= 0.5 – [(-2 + 2 + cos 0.5)/ (-4 – sin 0.4)]
= 0.5 – [cos 0.5/ (-4 – sin 0.5)]
= 0.5 – [0.8775/ (-4 – 0.4794)]
= 0.5 – (0.8775/-4.4794)
= 0.5 + 0.1958
= 0. 6958

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy