Symbolic computation : the derivative of a quotient.

X

xhunga

Symbolic computation : the derivative of a quotient.

Hello,

This time I try to simulate the derivative of a quotient.
The same function use the quotient rule if it is a quotient (f/g)
and the reciprocal rule if it is not a quotient. (1/g)

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: (try this first:Quotient rule )

@[ a*f(x) : b*g(x) ]

eq03.txt: (try this first:Reciprocal rule )

@[ a*f(b) : b*g(x) ]


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

is_a_quotient();

copy into "eq_n" the numerator.
copy into "eq_d" the denominator.


D_quotient();

Call the previous function,
and 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_N 1000
#define CHAR_A 2
/* ------------------------------------ */
int is_a_quotient(char *filename,char *eq_n,char *eq_d,char *x);
void D_quotient( char *filename,char *eq,char *x);
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
char filename[FILENAME_MAX];
char eq [CHAR_N];
char x [CHAR_A];

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

D_quotient(filename,eq,x);

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

getchar();
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
int is_a_quotient(
char *filename,
char *eq_n,
char *eq_d,
char *x
)
{
/* ------------------------------------ */
FILE *f;
/* ------------------------------------ */
char c[CHAR_A];
/* ------------------------------------ */
int c_int;
int d; /* denominator True:1 Fault:0*/
int y; /* x True:1 Fault:0*/
int q; /* quotient True:1 Fault:0*/
/* ------------------------------------ */
strcpy( c,"");
strcpy(eq_n,"");/* numerator */
strcpy(eq_d,"");/* denominator */
/* ------------------------------------ */
q = 0;
y = 0;
d = 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,"[" ))||
!(strcmp(c,"]" )) )

strcpy(c,"");

else{
if( !(strcmp(c,x )) ) y=1;

if( !(strcmp(c,":")) )
{
d=1;
if(y)q=1;/* it is a quotient */
}
else /* no : */
if(d==0) strcat(eq_n,c); /* numerator */
else strcat(eq_d,c); /* denominator */
}
}

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

return (q);
}

/* ------------------------------------ */
void D_quotient(
char *filename,
char *eq,
char *x
)
{
/* ------------------------------------ */
char eq_n [CHAR_N];
char eq_d [CHAR_N];
/* ------------------------------------ */

strcpy(eq,"");

if(is_a_quotient(filename,eq_n,eq_d,x))
{
strcat(eq," @[");
strcat(eq,eq_n);
strcat(eq,"] [");
strcat(eq,eq_d);
strcat(eq,"]\n-\n [");
strcat(eq,eq_n);
strcat(eq,"] @[");
strcat(eq,eq_d);
strcat(eq,"]\n\n:\n\n [");
strcat(eq,eq_d);
strcat(eq,"]^2\n");
}
else
{
strcat(eq,"- [");
strcat(eq,eq_n);
strcat(eq,"] @[");
strcat(eq,eq_d);
strcat(eq,"]\n\n:\n\n [");
strcat(eq,eq_d);
strcat(eq,"]^2\n");
}
}
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top