PC-CS-391 Lab
PC-CS-391 Lab
PC-CS-391 Lab
oflabhours
1 Numericalsolutionof centraldifferenceinterpolation by using
3
Lagrange interpolation method
2 NumericalsolutionofintegralsusingTrapezoidal & Simpson’s 3
method
3 NumericalSolutionofinitialvalueproblemusing Gauss Seidel 3
method
4 Solutionofsimultaneousnon-linearequationsusing Newton 3
Raphson Method(Two variables)
5 NumericalSolutionofinitialvalueproblemusingEuler’s & 3
RungaKutta method
Objective : - Solution of Gauss – Seidel Method.
FLOWCHART:
Start
Initializexto 0
Experiment No - 3
err=fabs(x[i]-t)
yes Maxerr=err
Is err>maxerr?
no
X[i]=t
EndLoop(i)
PrintResultsof
Iteration
yes
Is axerr<aerr
PrintSolution
no
EndLoop(itr)
Stop
PrintSolutiondoes
notconverge.
Stop
SourceCode:
Algorithm3.5(Gauss-Seidel-Iteration).
*/
#include<stdio.h>#include<stdlib.h>#incl
ude<math.h>
/* */
/*Mainprogramforalgorithm3.5*/
voidmain(void)
{
doubleTol=10E-6; double Sep= /* Tolerance */
1.0; /* Initialize */
intK =1; /* Counterfor iterations */
intMax=99; /* Maximumnumberofiterat. */
intCond=1; /* Conditionofmatrix */
intR,C,J; /* Loopcounters */
doubleA[Limit][Limit]; /* AinAX=B,INPUT */
doubleB[Limit]; /* BinAX=B,INPUT */
intN; /* DimensionofA,INPUT */
/* =Numberofequations */
doubleRow; /* Variableindominancecheck */
double P[Limit];
doublePold[Limit];
doubleSum;
printf("Yousaythereare%dequations.\n",N);
printf(" \n");
printf("FromAX=BentercomponentsofvectorBonebyone:\n"); for (R = 1; R <= N; R+
+)
{
printf("Enter%dst/nd/rdcomponentofvectorB\n",R); scanf("%lf", &B[R-1]);
}
printf(" \n");
printf("FromAX=BenterelementsofArowbyrow:\n");printf(" \n");
for(R=1;R<=N;R++)
{
for(J=1;J<=N;J++)
{
printf("Forrow%denterelement%dplease:\n",R,J); scanf("%lf", &A[R-1]
[J-1]);
}
}
/*Checkfordiagonaldominance.*/ for (R = 1; R
<= N; R++)
{
Row=0.0;
for(C=1;C<=N;C++)Row+=fabs(A[R-1][C-1]);
if(Row>=2.0*fabs(A[R-1][R-1]))Cond=0;
}
if(Cond==0)
{
printf("Thematrixisnotdiagonallydominant.\n"); printf("Cannot
apply this algorithm ---> exit\n"); exit(1);
}
/*output*/
if(Sep<Tol)printf("Thesolutiontothelinearsystemis:\n"); else printf("Gauss-Seidel
iteration did not
converge:\n");
for(J=1;J<=N;J++)printf("P[%d]=%lf\n",J,P[J-1]);
}
Experiment No - 1
Aim:NumericalsolutionofcentraldifferenceinterpolationbyusingLagrangeinterpolation method
Brief Theory:Interpolation isa method of finding new data points within the range ofa
discretesetofknowndata points.Inotherwordsinterpolationisthetechniquetoestimatethe value of
a mathematicalfunction, for anyintermediate value of the independent variable.
NumericalProcedure:
1. .Readn,X
2. Readxi,yi,wherei=1,2,3,…..,n
3. sum=0
4. fori=1(1) n,dotill(11)
5. prod= 1
6. forj=1(1) n,dotill(9)
7. ifj≠ i,
8. prod=prod*(X-x[j])/(x[i]-x[j])
9. Else,Nextj
10. sum=sum+(prod*y[i])
11. Nexti
12. PrintX,sum
13. stop
Examples:
i. Giventhefollowingdatatable,constructtheLagrangeinterpolation
i 0 1 2 3
0 1 2 3
ii. Giventhefollowingdatatable,constructtheLagrangeinterpolationpolynomialf(x),tofitthedata
and find f(1998)
i 0 1 2 3 4 5
iii. Findthevalueofywhenx=10,ifthefollowingvaluesofxandyaregiven:
5 6 9 11
12 13 14 16
Experiment No – 4(a)
Aim:Solutionofsimultaneousnon-linearequationsusingNewtonRaphsonMethod(Two variables).
BriefTheory:TheNewton-Raphsonsecondordermethodfortwo variable’sfunctioncanbe obtain
as follows
Thesolutionisobtainedwithanefficientlinearsolvingmethodlikethepartialpivotinggauss algorithm.
ThesecondorderTaylor'sdevelopmentgivestheexactapproximationofthefunction.
NumericalProcedure:
1. Considerasystemoftwoequationsintwovariables:f(x,y)=0g(x,y)=0.
2. Supposewehaveanapproximationforasolution(x0,y0)andwewouldliketocompute
∆xand∆ysox1= x0+∆xandy1= y0+ ∆y satisfythesystem:f(x1,y1)= f(x0+∆x, y0 + ∆y) =
0
3. g(x1,y1)=g(x0+∆x,y0+∆y)=0.compute∆xand∆y.
4. Taylorseriesintwovariablesf(x0+ ∆x,y0+∆y)= f(x0,y0)+∂f∂x(x0,y0)∆x+ ∂f∂y (x0,
y0)∆y + · · ·
5. g(x0+∆x,y0+ ∆y)= g(x0,y0)+∂g∂x(x0,y0)∆x+∂g∂y(x0,y0)∆y+ ···
6. where∂f∂x(x0,y0)and∂f∂y(x0,y0)arethepartialderivativesoffwith respecttox and y
evaluated at (x0, y0);
7. ∂g∂x(x0,y0)and∂g∂y(x0,y0)arethepartialderivativesofgwithrespecttoxandy evaluated
at (x0, y0);
8. andthe···representthehigherordertermsintheseries,in(∆x)2,(∆x)(∆y),and(∆y)2
.Because∆xand∆yarealreadysmallnumbers,thehigherordertermsareevensmaller.
9. Thensolvesystemofequationbyusingmatrixformofthesystemoftheequations.
Examples:
ii. Solvethesystemofnonlinearequation:
Brief Theory:
Numerical Procedure:
1. Start
2. DefineandDeclarefunction
3. Inputinitialboundaryvalue,finalboundaryvalueandlengthofinterval
4. Calculatenumberofstrips,n=(finalboundaryvalue–finalboundaryvalue)/lengthof
interval
5. Performfollowingoperationinloop
x[i]=x0+i*h
y[i]=f(x[i])
print y[i]
Initializese=0,s0=0
6. Dothefollowingusingloop
Ifi%2=0
So=s0+y[i]
Otherwise
Se=se+y[i]
ans=h/3*(y[0]+y[n]+4*so+2*se)
7. printtheans
8. stop
2
Examples:i.UseTrapezoidalrulewith6subintervalstoapproximate x2).
òdx/(16+
0
ii. UsemultiplesegmentTrapezoidalruletofindtheareaunderthecurve
f(x)=300x/(1+ex)fromx=0tox=10.
iii. UseTrapezoidalrulewithn=4,approximatethevalueoftheintegral
Experiment No – 2(b)
Aim:NumericalevaluationofintegralsusingSimpson’smethod
Brief Theory:C program for Simpson 1/3 rule for easy and accurate calculation of numerical
integration of any function which is defined in program. In the source code , a functionf(x) has
been defined. The calculation using Simpson 1/3 rule in C is based on the fact that the small
portion between any two points is a parabola. The program follows the following steps for
calculation of the integral.
NumericalProcedure:
1. Definef(x)
2. Enterthevaluesoflowerandupperlimitofx,i.e.x0andalsoenternumberofintervals, N(N
should be even number)
3. h=((xn–x0)/N)
4. sum=0
5. do
6. {
7. sum= sum+(h/3).[f(x0)+4f(x0+h)+f(x0+ 2h)]
8. x0=x0+ 2h
9. }while(x0<xn)
10. printsum
11. stop
Examples:
3
i. Approximate
òdx/(x+1)usingSimpson'sRulewithn=4.
2
1
ii. Computetheintegral 2
òe dx bySimpson'srule.
x
p/2
iii. Calculatethevalueof
òsinxdxusing11ordinates.
0
Experiment No – 5(a)
Aim:NumericalSolutionofinitialvalueproblembyusingEuler’smethod
BriefTheory:TheEulermethodisafirst-ordermethod,whichmeansthatthelocalerror(error per
step)isproportional tothesquareofthestepsize,andtheglobal error(errorat agiventime) is
proportional to the step size.
NumericalProcedure:
1. Start
2. Definefunction
3. Getthevaluesofx0,y0,hand xn
*Herex0andy0aretheinitialconditions h is
the interval
xnistherequiredvalue
4. n=(xn–x0)/h+1
5. Startloopfromi=1ton
6. y=y0+h*f(x0,y0) x
=x+h
7. Printvaluesofy0andx0
8. Check ifx<xn
Ifyes,assignx0=xandy0=y If no,
goto 9.
9. Endloopi
10. Stop
Examples:SolvetheInitialValueProblems
i. dy/dx=6−2(y/x),y(3)=1
ii. dy/dx=ylny/x,y(2)=e
iii. dy/dx=(y-x)/(y+x),y(0)=1,Findyforx=0.1.
Experiment No – 5(b)
Aim:NumericalSolutionofinitialvalueproblemusingRungaKuttamethod
Brief Theory:C program for RungeKutta 4 method is designed to find out the numerical
solution ofa first order differential equation. It is a kind ofinitial value problem in which initial
conditions are known, i.e the values of x 0 and y0are known, and the values of y at different
values x is to be found out.
NumericalProcedure:
1. Definef(x,y)
2. Enterthevalueofxo,y0,xn,h
3. do
{
k1=h.f(x0,y0)
k2 =
h.f(x0+h/2,y0+k1/2)k3=h.f(
x0+h/2,y0+k2/2) k4 =
h.f(x0 + h, y0 + k3)
k=(k1+2.k2+2.k3+k4)/6
y1=y0+k
printx0,y0
y0 = y1
x0=x0+h
}while(x0<xn)
4. stop
Examples:
i. UsingRKmethodoforderfourfindyatx=1.1and1.2bysolvingdy/dx=x^2+y^2,y(1)=2.3