Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Finding a derivative in C++

$
0
0
Hello !

As the title says , i can't seem to find out how to find a derivative of a function.

I have a project in which i first have to find the analitic derivative (as a text ) which i managed to do with lots of time lost, here is the code for it and it works perfectly :


#include<iostream>
using namespace std;
#include<math.h>


int main()
{	int Coeficient[100],i,liber[100],g,j,ajutor,w,help,h,m;
	float x;
	char Functie[100],Nou[100];

	cout<<"Introduceti o functie:"<<endl;
	cin>>Functie;
	g=strlen(Functie);
	j=0;
	for(i=0;i<=g;i++)
	{	
		
		if( (Functie[i]=='4') || (Functie[i]=='1') || (Functie[i]=='2') || (Functie[i]=='3') || (Functie[i]=='5') || (Functie[i]=='6') || (Functie[i]=='7') || (Functie[i]=='8') || (Functie[i]=='9') )
			{	
				h=i;
				do{
					h++;
					
				}while( (Functie[h]=='4') || (Functie[h]=='1') || (Functie[h]=='2') || (Functie[h]=='3') || (Functie[h]=='5') || (Functie[h]=='6') || (Functie[h]=='7') || (Functie[h]=='8') || (Functie[h]=='9') );
				
				if( ( (i == 0) || (Functie[i-1]=='(') || (Functie[i-1]=='+') || (Functie[i-1]=='-') ) && ( (Functie[h]==')') || (Functie[h]=='+') || (Functie[h]=='-') || (Functie[h]==NULL) ) )
					{
						Nou[j]='0';
						i=h-1;
					}
				else
					Nou[j]=Functie[i];
			}
		
		if(Functie[i]=='+')
		{
			Nou[j]=Functie[i];
		}
		
		if(Functie[i]=='(')
		{
			Nou[j]=Functie[i];
		}
		
		if(Functie[i]==')')
		{
			Nou[j]=Functie[i];
		}
		
		if( (Functie[i]=='s') && (Functie[i+1]=='i') && (Functie[i+2]=='n') )
		{
			Nou[j]='c';
			j++;
			Nou[j]='o';
			j++;
			Nou[j]='s';
			if(Functie[i+3]=='(')
			{
				j++;
				Nou[j]='(';
				help=i+4;
				while(Functie[help]!=')')
				{
					j++;
					Nou[j]=Functie[help];
					help++;
				}
				j++;
				Nou[j]=')';
				j++;
				Nou[j]='*';
			}
			else
			{
				j++;
				Nou[j]=Functie[i+3];
				j++;
				Nou[j]='*';
			}
			i++;
			i++;

		}
		if( (Functie[i]=='l') && (Functie[i+1]=='n') )
		{
			Nou[j]='(';
			j++;
			Nou[j]='1';
			j++;
			Nou[j]='/';
			
			if(Functie[i+2]=='(')
			{
				j++;
				Nou[j]='(';
				help=i+3;
				while(Functie[help]!=')')
				{
					j++;
					Nou[j]=Functie[help];
					help++;
				}
				j++;
				Nou[j]=')';
				j++;
				Nou[j]=')';
				j++;
				Nou[j]='*';
			}
			else
			{
				j++;
				Nou[j]=Functie[i+2];
				j++;
				Nou[j]=')';
				j++;
				Nou[j]='*';
			}
			i++;
		}
	
		if( (Functie[i]=='c') && (Functie[i+1]=='o') && (Functie[i+2]=='s') )
		{
			Nou[j]='-';
			j++;
			Nou[j]='s';
			j++;
			Nou[j]='i';
			j++;
			Nou[j]='n';
			if(Functie[i+3]=='(')
			{
				j++;
				Nou[j]='(';
				help=i+4;
				while(Functie[help]!=')')
				{
					j++;
					Nou[j]=Functie[help];
					help++;
				}
				j++;
				Nou[j]=')';
				j++;
				Nou[j]='*';
			}
			else
			{
				j++;
				Nou[j]=Functie[i+3];
				j++;
				Nou[j]='*';
			}
			i++;
			i++;

		}
					
		if(Functie[i]=='-')
		{
			Nou[j]=Functie[i];
		}
		
		if(Functie[i]=='/')
		{
			Nou[j]=Functie[i];
		}
		
		if(Functie[i]=='*')
		{
			Nou[j]=Functie[i];
		}
		
		if(Functie[i]=='x')
		{	
			if(Functie[i+1]=='^')
				Nou[j]=Functie[i];
			else
				Nou[j]='1';
		}
		
		if(Functie[i]=='^')
		{	
			Nou[j]=Functie[i];
			Coeficient[i]=Functie[i+1]-'0'-1;
			ajutor=Coeficient[i]+1;
			Functie[i+1]=(char)(((int)'0')+Coeficient[i]);
			j++;
			Nou[j]=Functie[i+1];
			j++;
			Nou[j]='*';
			j++;
			Nou[j]=(char)(((int)'0')+ajutor);
			i++;
		}
		j++;
	}
	
	cout<<"Functia este : "<<endl;
	for(i=0;i<j-1;i++)
		cout<<Nou[i];
	cout<<endl;
	system("PAUSE");
	
	return 0;
}



But now i have to solve the function .
For example lets say the function is f(x) = x^2 +x*4
I have to find the result in a point ,for example if x is 5 i have to compute the result of f(x)=45
If i find out how to do this ill solve the derivative in the same way.

Any tips would be apreciated!!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>