Maths Sci Lab
Maths Sci Lab
PAGE REMAR
S.NO DATE NAME OF THE PRACTICAL NO. KS
Practical -1
1.Write a program Regula falsi method using while loop
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining equation to be solved.
Change this equation to solve another problem. */
#define f(x) x*log10(x) - 1.2
int main()
{
if(f0*f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}while(fabs(f2)>e);
Practical -2
// While Loop
int count = 0;
while (count < 5) {
printf("%d ", count);
count++;
}
printf("\n");
// Do-While Loop
count = 0;
do {
printf("%d ", count);
count++;
} while (count < 5);
printf("\n");
return 0;
}
Output
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
Practical -3
1. Start
C = A−1B
7. Stop
Flow Chart for fitting a straight line to given set of data points
3. Write a program to fit a Straight Y=a+bx
#include<stdio.h>
#include<conio.h>
#define S 50
int main()
{
int n, i;
float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b;
clrscr();
/* Input */
printf("How many data points?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%f", &x[i]);
printf("y[%d]=",i);
scanf("%f", &y[i]);
}
/* Calculating Required Sum */
for(i=1;i<=n;i++)
{
sumX = sumX + x[i];
sumX2 = sumX2 + x[i]*x[i];
sumY = sumY + y[i];
sumXY = sumXY + x[i]*y[i];
}
/* Calculating a and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
a = (sumY - b*sumX)/n;
/* Displaying value of a and b */
printf("Values are: a=%0.2f and b = %0.2f",a,b);
printf("\nEquation of best fit is: y = %0.2f + %0.2fx",a,b);
getch();
return(0);
}
Practical-4
Fitting a Parabola
5.Solve the system of equations given by (1), (2) and (3) using matrix method, where
C = A−1B
6.Requiredparabola to be fitted is y = ax2 + bx + c
7.Stop
4.write a program to fit a parabola=ax2+bx+c
Program:
#include <stdio.h>
#include <math.h>
int main(void)
{
double a,b,c,root1,root2;
printf(" Please enter a \n");
scanf("%lf",&a);
printf(" Please enter b \n");
scanf("%lf",&b);
printf(" Please enter c \n");
scanf("%lf",&c);
root1 = (-b + sqrt(b*b-4.*a*c) ) / (2.*a);
root2 = (-b - sqrt(b*b-4.*a*c) ) / (2.*a);
printf("\n First root is %lf ",root1);
printf("\n Second root is %lf ",root2);
printf("\n ");
return 0;
}
OUTPUT:
Root 1 : 2
Root 2: 3
Root 3:
Practical -5
OUTPUT:
Coefficient a: 1.00
Coefficient b: 1.50
Practical -6
Problem: dy
dx =− y with the initial condition y (x=0)=1 find y(x) for 0 <x<2
#include <stdio.h>
#include <math.h>
int main()
{float k1, k2, x0, l, y0, h, x1, y1;
int n,i;
printf("enter the value of n \n");
scanf("%d",&n);
printf("enter the initial point x0, last point L and initial condition y0:\n");
scanf("%f %f %f",&x0,&l,&y0);
h=(l-x0)/n;
for(i=1;i<=n;i++)
{x1=x0+h;
k1=-h*y0;
k2=-h*(y0+k1);
y1=y0+0.5*(k1+k2);
printf("x[%d] and y[%d]:%f\t\t%f \n",i,i,x1,y1);
x0=x1;
y0=y1;}
return 0;}
Output of the program:
enter the value of n
5
enter the initial point x0, last point L and initial condition y0:
021
x[1] and y[1]:0.400000 0.680000
x[2] and y[2]:0.800000 0.462400
x[3] and y[3]:1.200000 0.314432
x[4] and y[4]:1.600000 0.213814
x[5] and y[5]:2.000000 0.145393
Practical -7
Euler’s Method
1. Start
2. Define and Declare function ydot representing dy
= f (x, y)
dx
3. Input x0, y0 , h and xn for the given initial value condition y(x0) = y0. h is
step size and xn is final value of x.
4. For x = x0 to xn solve the D.E. in steps of h using inbuilt function ode.
5. Print values of x0 to xn and y0 to yn
6. Plot graph in (x, y)
7. Stop
Flow Chart for Euler’s Method
7. write a program to implement the Euler’s Function
Program:
#include<stdio.h>
float fun(float x,float y)
{
float f;
f=x+y;
return f;
}
main()
{
float a,b,x,y,h,t,k;
printf("\nEnter x0,y0,h,xn: ");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
printf("\n x\t y\n");
while(x<=t)
{
k=h*fun(x,y);
y=y+k;
x=x+h;
printf("%0.3f\t%0.3f\n",x,y);
}
}
OUTPUT:
Practical -8
#include<stdio.h>
#include<math.h>
return x*x;
main(){
int n,i;
double a,b,h,x,sum=0,integral;
scanf("%d",&n);
scanf("%lf",&a);
scanf("%lf",&b);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%3==0){
sum=sum+2*f(x);
else{
sum=sum+3*f(x);
integral=(3*h/8)*(f(a)+f(b)+sum);
}
OUTPUT:
Practical -9
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
Output
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
Output:
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
1. Start
2. Read y = f (x) whose root is to be computed.
3. Input a and b where a, b are end points of interval (a, b) in
which the root lies.
4. Compute f (a) and f (b).
5. If f (a) and f (b) have same signs, then display function
must have different signs at a and b, exit. Otherwise go
to next step.
6. Input e and set iteration number counter to zero. Here e is
the absolute error i.e. the desired degree of accuracy.
7. root = (a + b)/2
8. If f (root) ∗ f (a) > 0, then a = root else b = root
9. Print root and iteration number
10. If |a − b| > 2e, print the root and exit otherwise continue in the
loop
11. Stop
Flow Chart for Bisection Method
11.Write a program to implement bisection program
Program:
#include<stdio.h>
#include<math.h>
int main()
{
printf("Studytonight - Best place to learn\n");
int iter;
printf("Enter the number of iteration you want to perform : ");
scanf("%d",&iter);
int ctr = 1;
double l1 = x0;
double l2 = x1;
double r,f1,f2,f3;
if(f2 == 0)
{
r = f2;
break; //Execution moves out of the while loop to the
statement just after it
}
printf("The root after %d iteration is %lf\n",ctr,r);
if(f1*f2 < 0)//Both are of opposite sign
l2 = r;
else if(f2*f3 < 0)
l1 = r;
ctr++;
}
}
f(x) = 10 - x^2
// menu-driven
do
{
// menu to choose the operation
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);
return 0;
}
Output:
Matrix A:
[[1 2 3]
[4 5 6]
[7 8 9]]
Matrix B:
[[9 8 7]
[6 5 4]
[3 2 1]]
Transpose of Matrix A:
[[1 4 7]
[2 5 8]
[3 6 9]]
Transpose of Matrix B:
[[9 6 3]
[8 5 2]
[7 4 1]]