Problem: We
have two functions of the type
y1
= exp ( -ax)
y2
= exp ( -ax2/2)
Plot the graphs of these functions for x varying from 0 to 5.0.
Problem Analysis: Initially when x = 0, y1 = y2 =1 and the
graphs start from the same point. The
curves cross when they are again equal at
x = 2.0. The program should have
appropriate branch statements to print the graph points at the following three
conditions:
1.
y1 > y2
2. y1
< y2
3.
y1 = y2
The functions y1 and y2 are normalized and converted to integers as
follows:
y1
= 50 exp ( -ax) + 0.5
y2
= 50 exp ( -ax2/2 ) +
0.5
The program in Fig.6.15 plots these two functions
simultaneously. ( 0 for y1, * for y2,
and # for the common point).
PLOTTING OF TWO
FUNCTIONS
Program
#include
<math.h>
main()
{
int i;
float
a, x, y1, y2;
a =
0.4;
printf("
Y -------> \n");
printf(" 0
-----------------------------------------\n");
for
( x = 0; x < 5; x = x+0.25)
{ /* BEGINNING OF FOR LOOP */
/*......Evaluation of functions .......*/
y1 = (int) ( 50 * exp( -a * x ) + 0.5 );
y2 = (int) ( 50 * exp( -a * x * x/2 ) + 0.5 );
/*......Plotting when y1 = y2.........*/
if ( y1 == y2)
{
if ( x == 2.5)
printf(" X |");
else
printf(" |");
for ( i = 1; i <= y1 - 1; ++i)
printf(" ");
printf("#\n");
continue;
}
/*...... Plotting when y1 > y2 .....*/
if ( y1 > y2)
{
if ( x == 2.5 )
printf(" X |");
else
printf(" |");
for ( i = 1; i <= y2 -1 ; ++i)
printf(" ");
printf("*");
for ( i = 1; i <= (y1 - y2 - 1); ++i)
printf("-");
printf("0\n");
continue;
}
/*........ Plotting when y2 >
y1.........*/
if ( x == 2.5)
printf(" X |");
else
printf(" |");
for ( i = 1 ; i <= (y1 - 1); ++i )
printf(" ");
printf("0");
for ( i = 1;
i <= ( y2 - y1 - 1 ); ++i)
printf("-");
printf("*\n");
} /*.......END OF FOR
LOOP........*/
printf(" |\n");
}
Output
Y
------->
0
----------------------------------------------------
|
#
|
0---*
|
0------*
|
0-------*
|
0------*
|
0------*
|
0----*
| 0-*
| #
| *-0
X
| *---0
| *-----0
| *------0
| *-------0
| *-------0
| *-------0
|
*-------0
|*-------0
|*------0
|*-----0
|
No comments:
Post a Comment