SJR October
SJR October
Algorithm:
Begin
Create two variables true_val and measured_val and take data from the user
set abs_err to the unsigned difference of true_val and measured_val
set rel_err to the ratio of abs_err over tru_val
print absolute error as abs_err and relative error as rel_err
End
Implementation in C:
Code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
}
Output
Set 2:
Set 2:
Algorithm:
Begin
Declare integers N, I, J, CH
Set CH:= 30
Declare float: ARR[10][11], PX, X, Y, P, H
Set PX:= 1
Input no of data and store in N
end for
end for
end for
Print value of f(X) stored in variable Y
End
Implementation in C:
Code
#include<stdio.h>
#include<math.h>
int fact(int);
main()
{
float arr[10][11],x,h,p,y,px=1;
int i,j,n,ch=30;
printf("\nEnter the number of data:");
scanf("%d",&n);
printf("\nEnter the values of x:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][0]);
}
printf("\nEnter the values of y:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][1]);
}
//Forming difference table.
for(j=2;j<=n;j++)
for(i=0;i<n-1;i++)
arr[i][j]=arr[i+1][j-1]-arr[i][j-1];
//Printing table
printf("\nDifference table is:-");
printf("\n\tX\tY");
for(i=0;i<=n-2;i++)
printf("\t%c^%dY",ch,i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
{
printf("\t%.2f",arr[i][j]);
}
}
//Take the value of x for f(x)
printf("\nEnter the value x for function f(x):");
scanf("%f",&x);
//Calculate the value of f(x) for x
h=arr[1][0]-arr[0][0];
p=(x-arr[0][0])/h;
y=arr[0][1];
for(i=1;i<n;i++)
{
px=px*(p-(i-1));
y=y+(arr[0][i+1]*px)/fact(i);
}
printf("\nthe value of function at x=%f is :%f",x,y);
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
Output
Algorithm:
Begin
Declare integers N, I, J, CH
Set CH:= 30
Declare float: ARR[10][11], PX, X, Y, P, H
Set PX:= 1
Input no of data and store in N
end for
end for
Code
#include<stdio.h>
int fact(int);
main()
{
int n,i,j,ch=30,k=1,w;
float arr[10][11],px=1,x,y,p,h;
printf("\nEnter the no of data:");
scanf("%d",&n);
w=n-1;
printf("\nEnter the values of x:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][0]);
}
printf("\nEnter the values of y:\n");
for(i=0;i<n;i++)
{
scanf("\n%f",&arr[i][1]);
}
//forming the difference table
for(j=2;j<=n;j++)
{
for(i=0;i<n-1;i++)
arr[i][j]=arr[i+1][j-1]-arr[i][j-1];
}
//printing table
printf("\nThe difference table is:\n");
printf("\tX \tY");
for(i=0;i<n-1;i++)
printf("\t%c^%d",ch,i+1);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n+1-i;j++)
printf("\t%.2f",arr[i][j]);
}
printf("\nEnter the value of x for f(x):");
scanf("%f",&x);
//calculate the value of f(x) for x
h=arr[n-1][0]-arr[n-2][0];
p=(x-arr[n-1][0])/h;
y=arr[n-1][1];
for(i=1;i<n;i++)
{
px=px*(p+(i-1));
y=y+(arr[n-1-i][i+1]*px)/fact(i);
}
printf("\nThe value of f(x) at x=%f is :%f",x,y);
}
int fact(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
return f;
}
Output
Set 1:
Set 2:
Algorithm:
Begin
Declare integers I, J, SIZE
Set K=1, N = 0
Input the size of the dataset
Input the corresponding Xi and Yi values
Input the value to be interpolated and set to X
[Interpolation]
for I:=0 to less than SIZE do
set num = deno = 1
for J:=0 to less than SIZE do
if J is not equal to I then
set num = num * (X * Xj)
set deno = Xi - Xj
end of if
end of for
set N = N + ((num / deno) * Yi)
end of for
Print interpolated value
End
Implementation in C:
Code
#include<stdio.h>
int main()
{
float x[10],y[10],num=1,deno=1,n=0,p;
int size;
int i,j,k=1;
printf("Enter the number of terms:\n");
scanf("%d",&size);
printf("Enter the values of x and y\n");
for(i=0;i<size;i++)
{
scanf("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("TABLE");
for(i=0;i<size;i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(k==1)
{
printf("Enter value of x to be interpolated: ");
scanf("%f",&p);
for(i=0;i<size;i++)
{
num=deno=1;
for(j=0;j<size;j++)
{
if(j!=i)
{
num*=p-x[j];
deno*=x[i]-x[j];
}
}
n=n+((num/deno)*y[i]);
}
printf("Interpolated value: %0.3f",n);
}
}
Output
Algorithm:
Begin
Input function f(x);
Read a,b,n; // the lower and upper limits and number of sub-intervals
Set h=(b-a)/n;
Set Sum = [f(a)-f(a+nh)];
for i=1 to n-1 step 2 do
Set sum = sum + 4*f(a+ih)+2*f(a+(i+1)h);
end for
Set result = sum * h/3;
Print result;
End
Implementation in C:
Code
#include <stdio.h>
double f(double x) {
return x*x;
}
int main() {
double a, b; int n;
printf("Enter the lower and upper limits: ");
scanf("%lf %lf",&a,&b);
printf("Enter number of intervals: ");
scanf("%d", &n);
double h = (b - a)/n;
double x = a + h;
double odd_sum = 0.0;
double even_sum = 0.0;
printf("Table:\n");
printf("X F(X)\n");
printf("%lf %lf\n", a, f(a));
for(int i=1; x<b; x+=h, i++) {
printf("%lf %lf\n", x, f(x));
if (i%2 == 1)
odd_sum+=f(x);
else
even_sum+=f(x);
}
printf("%lf %lf\n", b, f(b));
double res = h/3.0 * (2.0*even_sum + 4.0*odd_sum + f(a) +
f(b));
printf("Result of Integration by using Simpson's One-Third
Rule: %lf\n", res);
return 0;
}
Output
Algorithm:
Begin
End
Implementation in C:
Code
#include <stdio.h>
#include <math.h>
double f(double x) {
return x*x;
}
int main() {
double a, b; int n;
printf("Enter the lower and upper limits: ");
scanf("%lf %lf",&a,&b);
printf("Enter number of intervals: ");
scanf("%d", &n);
double h = (b - a)/n;
double x = a + h;
double sum = 0.0;
printf("Table:\n");
printf("X F(X)\n");
printf("%lf %lf\n",a, f(a));
for(; x<b; x+=h) {
printf("%lf %lf\n",x,f(x));
sum+=f(x);
}
printf("%lf %lf\n",b, f(b));
sum = h/2.0 * (2.0*sum + f(a) + f(b));
printf("Result: %lf\n", sum);
return 0;
}
Output