Symbolic computation : the derivative of a product.

X

xhunga

Hello,

This time I try to simulate the derivative of a product.

Into a text file,
I have an equation of this type.
I have created these equations
with the previous functions.

Examples:

eq01.txt:
@[a*cos(2*x+3*x^2)*cos(2*n+3*a^2)*b*cos(2*x+3*a^2)]

eq02.txt:
@[a*cos(x)*cos(a)*sin(x)]

eq03.txt: (try this first)
@[a*g(b)*f(x)*c*h(d)*h(x)*i(x)*k(x)]


The functions
=============

D_product_a();

copy into "eqconst" the constant function.
copy into "eqx" the function which depend of x.

strcat_D();

write the derivative of the function (simulate) (sign @)

D_product();

Call the two previous functions,
to do the work.


See below my work.

Have I do some mistakes?

The result seem correct.

Thank.

/* ------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ------------------------------------ */
#define CHAR_MAX 1000
#define CHAR_A 2
/* ------------------------------------ */
void strcat_D(char *eq,char *eqx,int i);
int D_product_a(char *filename,char *eqx,char *eqconst,char *x);
void D_product(char *filename,char *eq,char *x);
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
char filename[FILENAME_MAX];
char eq[CHAR_MAX];
char x[CHAR_A];

strcpy(filename,"eq03.txt");
strcpy(x,"x");

D_product(filename,eq,x);

printf("\n\n%s \n\n",eq);

getchar();
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
int D_product_a(
char *filename,
char *eqx,
char *eqconst,
char *x
)
{
/* ------------------------------------ */
FILE *f;
/* ------------------------------------ */
char t[CHAR_MAX];
char c[CHAR_A];
/* ------------------------------------ */
int c_int;
int p;
int y;
int n_product;
/* ------------------------------------ */
strcpy( t,"");
strcpy( c,"");
strcpy(eqx,"");
/* ------------------------------------ */
p=0;
y=0;
n_product=0;
/* ------------------------------------ */

f = fopen(filename,"r");

if (f==NULL) printf("File doesn't exist\n");
else
{
while( (c_int=getc(f))!=EOF )
{
c[0]=c_int;


if( !(strcmp(c,"\n"))||
!(strcmp(c," " ))||
!(strcmp(c,"@" ))||
!(strcmp(c,"[" )) )

strcpy(c,"");

else{
if( !(strcmp(c,"(")) ) p++;
else if( !(strcmp(c,")")) ) p--;
else if( !(strcmp(c,x )) ) y=1;

if( (!strcmp(c,"*")||
!strcmp(c,"]"))&&
!p )/* p==0 true */
{
if(y==0)
{
strcat(eqconst,t);
strcpy(t,"");
}
else
{
t[0]=' ';
strcat(eqx," ");
strcat(eqx,"[");
strcat(eqx,t);
strcat(eqx,"]");
strcpy(t,"");
++n_product;
y=0;
}
}
strcat(t,c);
}
}
}

if(f!=NULL)fclose(f);

return(n_product);
}

/* ------------------------------------ */
void strcat_D(
char *eq,
char *eqx,
int i)
{
/* ------------------------------------ */
int n;
/* ------------------------------------ */
n=1;
/* ------------------------------------ */

for (;*eq;++eq);

while ((*eqx++))
{
if( (*eqx =='['))
{
if(n==i)*eq++ ='@';
else *eq++ =' ';
n++;
}
*eq++ = *eqx;
}
}

/* ------------------------------------ */
void D_product(
char *filename,
char *eq,
char *x
)
{
/* ------------------------------------ */
char t [CHAR_MAX];
char eqx [CHAR_MAX];
char eqconst[CHAR_MAX];
/* ------------------------------------ */
int i;
/* ------------------------------------ */

strcpy(eq,"");

for(i=D_product_a(filename,eqx,eqconst,x); i;i--)
{
strcat(eq,"[");
strcat(eq,eqconst);
strcat(eq,"]");
strcat_D(eq,eqx,i);
if(i>1)strcat(eq,"+\n");
}
}
 
U

user923005

#include <string.h> {snip}
void strcat_D(

Here's another thing.
Function names starting with str* belong to the implementation.
Call it something like myStrcat().
 
X

xhunga

user923005 a écrit :
Here's another thing.
Function names starting with str* belong to the implementation.
Call it something like myStrcat().

thank for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top