Tải bản đầy đủ (.pdf) (10 trang)

A textbook of Computer Based Numerical and Statiscal Techniques part 59 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (90.34 KB, 10 trang )

566
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
b = x1;
}
x1 = (a + b)/2;
printf(“%f”,x1);
}
The root of the given equation is 1.5121.
13.4 ALGORITHM FOR FALSE POSITION METHOD
Step 1. Start of the program to compute the real root of the equation
Step 2. Input the value of x
0
, x
1
and e
Step 3. Check f(x
0
) × f(x
1
) < 0
Step 4. If no, print “Error” and exit
Step 5. If yes, compute x
2
=
xfx xfx
fx fx
01 10
10
>C >C
>C >C



Step 6. Compute f(x
2
)
Step 7. Again, if f(x
2
) × f(x
0
) < 0
Step 8. Set x
1
= x
2
Step 9. Else, set x
0
= x
2
Step 10. Continue the process step 5 to step 9 till to get required accuracy
Step 11. Print output
Step 12. End of the program.
13.5 PROGRAMMING FOR FALSE POSITION METHOD
(1) Find the Real Root of the Given Equation x
3
 2x  5 = 0
#include<conio.h>
#include<stdio.h>
#include<math.h>
void false(float, float);
void main()
{

float x0 = 3, x1 = 4;
clrscr();
false(x0, x1);
getch();
}
void false(float x0, float x1)
{
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
567
int i;
float x2 = 0, a = 0, b = 0, c = 0;
for(i = 0; i < 12; i++)
{
a = pow(x0, 3)–2*x0–5;
b = pow(x1, 3)–2*x1–5;
x2 = x0–(x1–x0)/(b–a)*a);
c = pow(x2, 3)–2*x2–5;
if(c < 0)
x0 = x2;
else
x1 = x2;
}
printf(“%f”, x2);
}
The root of the given equation is 2.094.
(2) Find the Real Root of the Given Equation 3x + sin x  e
x
= 0
#include<conio.h>
#include<stdio.h>

#include<math.h>
float flase(float, float);
void main()
{
float a, x0 = 0, x1 = 1, b, x2;
clrscr();
a = false(x0, x1);
b = false(x2);
printf(“%f”, b);
getch();
}
float false(float x0, float x1)
{
float x2;
int i;
for(i = 1; i <= 13; i++)
{
y0 = 3*x0 + sin(x0)–pow(2.7187, x0);
y1 = 3*x1 + sin(x1)–pow(2.7187, x1);
x2 = x0–(x1–x0)/(y1–y0)*y0;
y2 = 3*x2 + sin(x2) – pow(2.7187, x2);
if(y2 < 0)
568
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
x0 = x2;
else
x1 = x2;
}
return (x2);
}

The root of the given equation is 36042.
13.6 ALGORITHM FOR ITERATION METHOD
Step 1. Start of the program to compute the real root of the equation
Step 2. Input the value of x
0
(initial guess)
Step 3. Input the value of required alllowed error e
Step 4. Input the total iteration to be allowed n
Step 5. Compute φ(x
0
), x
1
← φ(x
0
)
(step 7 to 8 are repeated until the procedure converges to a root)
Step 6. For i = 1 to n, in step 2 to step 4 do
Step 7. x
0
← x
1
, x
1
← φ(x
0
)
Step 8. If
xx
x
10

1

≤ e then GOTO step 11
end for
Step 9. Print “does not converge to a root”, x
0
, x
1
Step 10. Stop
Step 11. Print “converge to a roof ”, i, x
1
Step 12. End of the program.
13.7 PROGRAMMING FOR ITERATION METHOD
(1) Find the Real Root of the Given Equation xe
x
= 1
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void iterat(float);
int i, j;
float x0, a;
clrscr();
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
569
a = 0.5;
x0 = a;
iterat(x0);

getch();
}
void iterat(float x0)
{
int i;
float x1, x2;
for(i = 1; i <= 12; i++)
{
x1 = 1/pow(e, x0);
x2 = 1/pow(e, x1);
x0 = x2;
}
print(“The result is: %f”, x2);
}
The root of the given equation is 0.5671.
(2) Find the Real Root of the Given Equation 2x log
10
x = 7
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void iterat(float);
int i, j;
float x0, a;
clrscr();
a = 3.7;
x0 = a;
iterat(x0);

getch();
}
void iterat(float x0)
{
int i;
float x1, x2;
for(i = 1; i <= 12; i++)
{
570
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
x1 = (7 + log(x0)/2;
x2 = (7 + log(x1)/2;
x0 = x2;
}
print(“The result is: %f”, x2);
}
The root of the given equation is 4.2199.
(3) Find the Real Root of the Given Equation x sin x = 1
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
void iterat(float);
int i, j;
float x0, a;
clrscr();
a = 1.5;
x0 = a;
iterat(x0);

getch();
}
void iterat (float x0)
{
int i;
float x1, x2;
for (i = 1; i <= 12; i++)
{
x1 = 1/sin (x0);
x2 = 1/sin (x1);
x0 = x2;
}
printf(“The result is: %f”, x2);
}
The root of the given equation is 1.114.
(4) Find the Real Root of the Given Equation 2x  log
10
x = 7
#include<stdio.h>
#include<conio.h>
#include<math.h>
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
571
float iteration(float);
void main()
{
float a;
float x = 3.7;
clrscr();
a = iteration(x);

printf(“%f”, a);
getch();
}
float iteration(float x)
{
int i;
float s = 0;
for(i = 0; i < 15; i++)
{
s = 0.5*(7 + log(x));
x = s;
}
return(s);
}
The root of the given equation is 4.2199.
13.8 ALGORITHM FOR NEWTON’S RAPHSON METHOD
Step 1. Start of the program to compute the real root of the equation
Step 2. Input the value of x
0
, n and e
Step 3. For i = 1 and repeat if i < = n
Step 4. f 0 = f(x
0
)
Step 5. df 0 = df(x
0
)
Step 6. Compute x1 = x
0
– (f 0/df 0)

Step 6a. If
xx
x
10
1

< e
Step 6b. Print “convergent”
Step 6c. Print x1, f(x1), i
Step 7. End of the program
Step 8. Else, Set x
0
= x1
Step 9. Repeat the process until to get required accuracy
Step 10. End of the program.
572
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
13.9 PROGRAMMING FOR NEWTON RAPHSON METHOD
(1) Find the Real Root of the Given Equation x
2
= 12
//program-netwon raphson
#include<conio.h>
#include<stdio.h>
#include<math.h>
float f(float x)
{
return((x*x)–(12));
}
float d(float x)

{
return((2*x));
}
void main()
{
float x, y, s;
int i, c = 0;
clrscr();
for(i = 0; ; i++)
{
if(f(i) > 0)
break;
}
x = i;
aa:
{
++c;
y = x–(f(x)/d(x));
x = y;
s = (y*10000);
printf(“\nthe position of iteration %d”, c);
printf(“\nthe root is %f”, y);
y = (y*10000);
if(y != s)
goto aa;
}
printf(“\nreal root is %f”,y);
getch();
}
The root of the given equation is 3.4641.

COMPUTER PROGRAMMING IN ‘C’ LANGUAGE
573
(2) Find the Real Root of the Given Equation x
2
 5x + 2 = 0
//program-netwon raphson
#include<conio.h>
#include<stdio.h>
#include<math.h>
float f(float x)
{
return((x*x) – (5*x) + 2;
}
float d(float x)
{
return((2*x) – 5);
}
void main()
{
float x, y = 0;
int i;
clrscr();
for(i = 0;; i++)
{
if(f(i) > 0)
break;
}
x = i;
for(i = 0; i < 10; i++)
{

y = x–f(x)/d(x);
x = y;
printf(“\nreal root is %f”, y);
}
getch();
}
The root of the given equation is 0.438447.
13.10 PROGRAMMING FOR MULLER’S METHOD
(1) Find the Real Root of the Given Equation x
3
 x
2
 x  1 = 0
#include<conio.h>
#include<stdio.h>
574
COMPUTER BASED NUMERICAL AND STATISTICAL TECHNIQUES
#include<math.h>
void main()
{
int i, j;
flat x0, x1, x2, x3, y0, y1, y2, a, b;
float val(float);
clrscr();
x0 = 1.9;
x1 = 2.0;
x2 = 2.1;
for(i = 0; i < 3; i++)
{
y0 = val(x0);

y1 = val(x1);
y2 = val(x2);
a = ((x0–x1)*(y1–y2)–(x1–x2)) (y0–y2))/((x1–x0)*(x1–x2)*(x0–x2));
b = (pow((x0–x1), 2)*(y1–y2)–pow((x1–x2), 2)*(y0–y2))/((x0–x1)*(x1–x2)*(x0–x2));
x3 = x2–((2*y2)/(b + pow((pow(b, 2)–4*a*y2), .5)));
x0 = x1;
x1 = x2;
x2 = x3;
}
printf(“%f”, x3);
getch();
}
float val(float x)
{
float y;
y = pow(x, 3) – pow(x, 2) – x – 1;
return(y);
}
The root of the given equation is 1.8382067.
(2) Find the Real Root of the Given Equation x
3
 3x  5 = 0
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int i, j;
float x0, x1, x2, x3, y0, y1, y2, a, b;
COMPUTER PROGRAMMING IN ‘C’ LANGUAGE

575
float val(float);
clrscr();
x0 = 1.9;
x1 = 2.0;
x2 = 2.7;
for(i = 0; i < 3; i++)
{
y0 = val(x0);
y1 = val(x1);
y2 = val(x2);
a = ((x0–x1)*(y1–y2)–(x1–x2)*(y0–y2))/((x1–x0)*(x1–x2)*(x0–x2));
b = (pow((x0–x1), 2)*(y1–y2)–(pow(x1–x2), 2)*(y0–y2))/((x0–x1)*(x1–x2)*(x0–x2));
x3 = x2–((2*y2)/(b + pow((pow(b, 2)–4*a*y2),. 5)));
x0 = x1;
x1 = x2;
x2 = x3;
}
printf(“%f”, x3);
getch();
}
float val(float x)
{
float y;
y = pow(x, 3)–(3
*
x) –5;
return(y);
}
The root of the given equation is 2.417728.

13.11
ALGORITHM FOR NEWTON’S FORWARD INTERPOLATION
METHOD
Step 1. Start of the program to interpolate the given data
Step 2. Input the value of n (number of terms)
Step 3. Input the array ax for data of x
Step 4. Input the array ay for data of y
Step 5. Compute h = ax[1] – ax[0]
Step 6. For i = 0; i < n–1; i++
Step 7. diff[i] [1] = ay[i+1]–ay[i]
Step 8. End of the loop i
Step 9. For j = 2; j <= 4; j++

×