Professional Documents
Culture Documents
Numerik
Numerik
o Bisection Method
o Regula-Falsi (False Position) Method
o Newton-Raphson Method
o Secant Method
For solving simultaneous equations
o Gauss Elimination Method
o Gauss-Seidel Method
o Jacobi Method
Interpolation
o Newton's Forward Difference Method
o Newton's Backward Difference Method
o Newton's Divided Difference Method
Numerical Differentiation
o Euler's Method
o Euler's Modified Method (Predictor-Corrector formula)
o Runge-Kutta 4th Order Method
Numerical Integration
o Trapezoidal Method
o Simpson's 3/8th Rule Method
Bisection Method
program bisection_method;
var
x1,x2,x3:real;
acc:integer;
function f(t:real):real;
begin
f:=t*t*t-t-1;
{the required function goes here. in this case it's
f(x)= x^3 - x - 1 }
end;
begin
writeln('Bisection method');
readln;
end.
function f(t:real):real;
begin
f:=t*t*t-2*t-5;
{the required function goes here. in this case it's
f(x)= x^3 - 2x - 5 }
end;
begin
writeln('False Position method');
writeln('enter the 1st start value: ');
readln(x1);
writeln('enter the 2nd start value: ');
readln(x2);
readln;
end.
function f(t:real):real;
begin
f:=t*sin(t)+cos(t);
end;
function fd(t:real):real;
begin
fd:=t*cos(t);
end;
begin
writeln('Newton-Rhapson method');
writeln('enter the start value: ');
readln(x1);
if fd(x1)=0 then
writeln('Invalid starting points')
else
begin
writeln('to how many decimal places? ');
readln(acc);
while (abs(f(x1))>1/exp(acc*(ln(10)))) do
begin
x1:=x1-f(x1)/fd(x1);
end;
writeln('The answer is: ',x1:5:acc+1);
end;
readln;
end.
Secant Method
program secant;
var
x1,x2,x3:real;
acc:integer;
function f(t:real):real;
begin
f:=t*t*t-2*t*t+t+10;
end;
begin
writeln('Secant method');
writeln('enter the 1st start value: ');
readln(x1);
writeln('enter the 2nd start value: ');
readln(x2);
if f(x1)=f(x2) then
writeln('Invalid starting points')
else
begin
writeln('to how many decimal places? ');
readln(acc);
while (abs(f(x2))>1/exp(acc*(ln(10)))) do
begin
x3:=(x1*f(x2)-x2*f(x1)) / (f(x2)-f(x1));
x1:=x2;x2:=x3;
end;
writeln('The answer is: ',x3:5:acc+1);
end;
readln;
end.
const MAXX=4;
const MAXEQNS=MAXX; {MAXEQNS should be = MAXX}
const SIGPLACES=4;
const DECPLACES=4;
begin
clrscr;
{write the equations in general form}
for i:=1 to MAXEQNS do
begin
for j:=1 to MAXX do
begin
write('a',i,j,'.x',j);
if j < MAXX then write(' + ');
end;
writeln(' = b',i);
end;
readln;
clrscr;
{answers}
writeln;
for i:=1 to MAXX do
write('x',i,' = ',x[i]:SIGPLACES:DECPLACES,' ');
readln;
end.
Gauss-Seidel Method
program gauss_seidel;
uses crt;
const MAXX=4;
const MAXEQNS=MAXX; {MAXEQNS should be = MAXX}
const SIGPLACES=4;
var a:array[1..MAXEQNS,1..MAXX+1] of real;
x:array[1..MAXX] of real;
fl:array[1..MAXX] of integer;
max:real;
i,j,acc,flag:integer;
procedure iterate;
var k,m:integer;
xtemp:real;
begin
flag:=0;
for k:=1 to MAXEQNS do
begin
xtemp:=x[k];
x[k]:=a[k,MAXX+1] / a[k,k];
for m:=1 to MAXX do
if k<>m then x[k]:=x[k] - a[k,m]/a[k,k]*x[m];
write(x[k]:SIGPLACES:acc,' ');
if abs(xtemp-x[k]) < 1/(exp(acc*ln(10))) then fl[k]:=1;
end;
writeln;
for k:=1 to MAXX do if fl[k]=1 then flag:=flag+1
begin {main()}
clrscr;
readln;
clrscr;
write('enter the number of decimal places to which you want the answer: ');
readln(acc);
writeln;
writeln('the given equations are:');
writeln;
{write the given eqns}
for i:=1 to MAXEQNS do
begin
for j:=1 to MAXX do
begin
write(a[i,j]:SIGPLACES:acc,' x',j);
if j < MAXX then write(' + ');
end;
writeln(' = ',a[i,MAXX+1]:SIGPLACES:acc);
end;
readln;
clrscr;
{seed}
flag:=0;
for i:=1 to MAXX do
begin
x[i]:=0;
fl[i]:=0;
end;
{iterate}
while flag < MAXX do iterate;
readln;
clrscr;
Jacobi Method
program jacobi;
uses crt;
const MAXX=4;
const MAXEQNS=MAXX; {MAXEQNS should be = MAXX}
const SIGPLACES=4;
procedure iterate;
var xtemp:array[1..MAXX] of real;
k,m:integer;
begin
for k:=1 to MAXEQNS do
begin
xtemp[k]:=a[k,MAXX+1] / a[k,k];
for m:=1 to MAXX do
if k<>m then xtemp[k]:=xtemp[k] - a[k,m]/a[k,k]*x[m];
write(xtemp[k]:SIGPLACES:acc,' ');
if abs(xtemp[k]-x[k]) < 1/(exp(acc*ln(10))) then flag:=flag+1;
end;
writeln;
for k:=1 to MAXX do x[k]:=xtemp[k];
begin
clrscr;
{write the equations in general form}
for i:=1 to MAXEQNS do
begin
for j:=1 to MAXX do
begin
write('a',i,j,'.x',j);
if j < MAXX then write(' + ');
end;
writeln(' = b',i);
end;
write('enter the number of decimal places to which you want the answer: ');
readln(acc);
writeln;
writeln('the given equations are:');
writeln;
{write the given eqns}
for i:=1 to MAXEQNS do
begin
for j:=1 to MAXX do
begin
write(a[i,j]:SIGPLACES:acc,' x',j);
if j < MAXX then write(' + ');
end;
writeln(' = ',a[i,MAXX+1]:SIGPLACES:acc);
end;
readln;
clrscr;
{seed}
x[1]:=a[1,MAXX+1] / a[1,1];
for i:=2 to MAXX do x[i]:=0;
{iterate}
flag:=0;
while flag < MAXX do iterate;
readln;
clrscr;
for i:=1 to MAXX do
writeln('x',i,' = ',x[i]:SIGPLACES:acc);
readln;
end.
const SIGPLACES=4;
DECPLACES=4;
MAXDATA=100;
MAXDELS=10;
var
n,i,j:integer;
x:array[1..MAXDATA] of real;
y:array[1..MAXDATA,1..MAXDELS] of real;
r,xn,yn,h:real;
function fact(num:longint):longint;
begin
if num>0 then fact:=num*fact(num-1) else fact:=1;
end;
function mult(num:real;subtract:integer):real;
begin
if subtract>0 then mult:=(num-subtract)*mult(num,subtract-1) else mult:=num;
end;
begin
write('how many data points? ');
readln(n);
write('enter starting value of x: ');
readln(x[1]);
write('enter the x interval: ');
readln(h);
for i:=1 to n do
begin
x[i]:=x[1]+(i-1)*h;
write('enter y',i,': ');
readln(y[i,1]);
end;
for i:=1 to n do
begin
write(x[i]:SIGPLACES:DECPLACES,' ');
for j:=1 to i do
write(y[i,j]:SIGPLACES:DECPLACES,' ');
writeln;
end;
writeln;writeln;
yn:=y[1,1];
r:=(xn-x[1])/h;
for i:=2 to MAXDELS do
yn:=yn+mult(r,i-2)/fact(i-1)*y[i,i];
writeln('answer: ',yn:SIGPLACES:DECPLACES);
readln;
end.
Back to list of programs
var
n,i,j:integer;
x:array[1..MAXDATA] of real;
y:array[1..MAXDATA,1..MAXDELS] of real;
r,xn,yn,h:real;
function fact(num:longint):longint;
begin
if num>0 then fact:=num*fact(num-1) else fact:=1;
end;
function mult(num:real;subtract:integer):real;
begin
if subtract>0 then mult:=(num+subtract)*mult(num,subtract-1) else mult:=num;
end;
begin
clrscr;
write('how many data points? ');
readln(n);
write('enter starting value of x: ');
readln(x[1]);
write('enter the x interval: ');
readln(h);
for i:=1 to n do
begin
x[i]:=x[1]+(i-1)*h;
write('enter y',i,': ');
readln(y[i,1]);
end;
for i:=1 to n do
begin
write(x[i]:SIGPLACES:DECPLACES,' ');
for j:=1 to i do
write(y[i,j]:SIGPLACES:DECPLACES,' ');
writeln;
end;
writeln;writeln;
yn:=y[n,1];
r:=(xn-x[n])/h;
for i:=2 to MAXDELS do
yn:=yn+mult(r,i-2)/fact(i-1)*y[n,i];
writeln('answer: ',yn:SIGPLACES:DECPLACES);
readln;
end.
Back to list of programs
var
n,i,j:integer;
x:array[1..MAXDATA] of real;
y:array[1..MAXDATA,1..MAXDELS] of real;
xn,yn:real;
function mult(k:integer):real;
begin
if k>1 then mult:=mult(k-1)*(xn-x[k]) else mult:=xn-x[1];
end;
begin
clrscr;
write('how many data points? ');
readln(n);
for i:=1 to n do
begin
write('enter x',i,': ');
readln(x[i]);
write('enter y',i,': ');
readln(y[i,1]);
end;
readln;
clrscr;
writeln;writeln;
yn:=y[1,1];
for i:=2 to MAXDELS do
yn:=yn+mult(i-1)*y[i,i];
writeln('answer: ',yn:SIGPLACES:DECPLACES);
readln;
end.
Back to list of programs
Euler's Method
program euler; {for diff eqns}
uses crt;
const SIGPLACES=4;
DECPLACES=4;
MAXDATA=100;
function f(x:real;y:real):real;
begin
f:=x+2*y; {given function}
end;
begin
clrscr;
write('enter x0: '); readln(x[0]);
write('enter y0: '); readln(y[0]);
write('enter the interval h: '); readln(h);
write('enter the value of x for which y is required (xn): ');
readln(xn);
i:=0;
repeat
begin
i:=i+1;
x[i]:=x[0]+i*h;
y[i]:=y[i-1] + (h * f(x[i-1],y[i-1]));
writeln(x[i]:SIGPLACES:DECPLACES,' ',y[i]:SIGPLACES:DECPLACES);
end;
until ((i=MAXDATA) or (x[i]=xn));
readln;
end.
Back to list of programs
function f(x:real;y:real):real;
begin
f:=x+3*y; {given function}
end;
begin
clrscr;
write('enter x0: '); readln(x[0]);
write('enter y0: '); readln(y[0]);
write('enter the interval h: '); readln(h);
write('enter the value of x for which y is required (xn): '); readln(xn);
i:=0;
repeat
begin
i:=i+1;
y[i]:=y[i-1] + (h * f(x[i-1],y[i-1]));
x[i]:=x[i-1]+h;
y[i]:=y[i-1] + (h/2) * ( f(x[i-1],y[i-1])+f(x[i],y[i]) );
writeln(xn:SIGPLACES:DECPLACES,' ',y[i]:SIGPLACES:DECPLACES);
end;
until (x[0]+(i*h)=xn);
readln;
end.
Back to list of programs
function f(x:real;y:real):real;
begin
f:=-y; { given function y = e^(-x) }
end;
begin
clrscr;
write('enter the first x value (x0): '); readln(x[0]);
write('enter the x value for which y is required (xn): '); readln(xn);
write('enter the step value (h): '); readln(h);
i:=0;
repeat
begin
k1:=h * f(x[i],y[i]);
k2:=h * f(x[i]+h/2,y[i]+k1/2);
k3:=h * f(x[i]+h/2,y[i]+k2/2);
k4:=h * f(x[i]+h,y[i]+k3);
i:=i+1;
x[i]:=x[0]+h*i;
y[i]:=y[i-1] + (k1+2*(k2+k3)+k4)/6;
writeln(x[i]:SIGPLACES:DECPLACES,' ',y[i]:SIGPLACES:DECPLACES);
end;
until ((i=MAXDATA) or (x[i]=xn));
readln;
end.
Back to list of programs
Trapezoidal Method
program trapezoidal;
uses crt;
const SIGPLACES=4;
DECPLACES=4;
var x1,xn,h,sum:real;
i:integer;
function f(x:real):real;
begin
f:=1/(1+x);
end;
begin
clrscr;
writeln('Trapezoidal method of numerical integration');
write('what is the starting value of x? ');
readln(x1);
write('what is the ending value of x? ');
readln(xn);
write('enter the interval h: ');
readln(h);
sum:=f(x1)+f(xn);
i:=1;
repeat
begin
i:=i+1;
sum:=sum+2*f(x1+(i-1)*h);
end;
until (x1+i*h = xn);
writeln('answer is ',h*sum/2:SIGPLACES:DECPLACES);
readln;
end.
Back to list of programs
const SIGPLACES=4;
DECPLACES=4;
MAXDATA=100;
begin
clrscr;
write('how many data points? ');
readln(n);
readln;
end.
Back to list of programs
Contact