sábado, 3 de março de 2012
metodos bissecção , newton , secantes e haley
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
double funcao3(double x)
{
return pow(x,5)-6;
}
double funcao2(double x)
{
return (2*cos(x))-pow(2.718281,x/2);
}
double funcao1(double x)
{
return (x/2)-tan(x);
}
double derifuncao1(double x)
{
return (1/2)-(1/(cos(x)*cos(x)));
}
double derifuncao2(double x)
{
return (-2*sin(x))-(pow(2.718281,x/2))/2;
}
double derifuncao3(double x)
{
return 5*pow(x,4.0);
}
double derisecfuncao1(double x)
{
return (-2*sin(x))/pow(cos(x),3);
}
double derisecfuncao2(double x)
{
return (-2*cos(x))-(pow(2.718281,x/2))/4;
}
double derisecfuncao3(double x)
{
return 20*pow(x,3.0);
}
double bisseccao3(double a,double b,double ERRO)
{
double p=a+((b-a)/2);
if(((b-a)/2)< ERRO)
{
return p;
}
else
{
if(funcao3(a)*funcao3(p)< 0)
return bisseccao3(a, p,ERRO);
else
return bisseccao3(p, b,ERRO);
}
}
double bisseccao2(double a,double b,double ERRO)
{
double p=a+((b-a)/2);
if(((b-a)/2)< ERRO)
{
return p;
}
else
{
if(funcao2(a)*funcao2(p)< 0)
return bisseccao2(a, p,ERRO);
else
return bisseccao2(p, b,ERRO);
}
}
double bisseccao1(double a,double b,double ERRO)
{
double p=a+((b-a)/2);
if(((b-a)/2)< ERRO)
{
return p;
}
else
{
if(funcao1(a)*funcao1(p)< 0)
return bisseccao1(a, p,ERRO);
else
return bisseccao1(p, b,ERRO);
}
}
double newton1(double x0,double ERRO)
{
double x;
x=x0-(funcao1(x0)/derifuncao1(x0)) ;
if(sqrt(pow((((x-x0)/x)),2))< ERRO)
return x;
else
return newton1( x, ERRO);
}
double newton2(double x0,double ERRO)
{
double x;
x=x0-(funcao2(x0)/derifuncao2(x0)) ;
if(sqrt(pow((((x-x0)/x)),2))< ERRO)
return x;
else
return newton2( x, ERRO);
}
double newton3(double x0,double ERRO)
{
double x;
x=x0-(funcao3(x0)/derifuncao3(x0)) ;
if(sqrt(pow((((x-x0)/x)),2))< ERRO)
return x;
else
return newton3( x, ERRO);
}
double secante1(double x0,double x1,double ERRO)
{
double x2;
x2= ((x0*funcao1(x1))-(x1*funcao1(x0)))/(funcao1(x1)-funcao1(x0));
if(sqrt(pow((((x2-x1)/x2)),2))< ERRO)
return x2;
else
return secante1( x1, x2, ERRO);
}
double secante2(double x0,double x1,double ERRO)
{
double x2;
x2= ((x0*funcao2(x1))-(x1*funcao2(x0)))/(funcao2(x1)-funcao2(x0));
if(sqrt(pow((((x2-x1)/x2)),2))< ERRO)
return x2;
else
return secante2( x1, x2, ERRO);
}
double secante3(double x0,double x1,double ERRO)
{
double x2;
x2= ((x0*funcao3(x1))-(x1*funcao3(x0)))/(funcao3(x1)-funcao3(x0));
if(sqrt(pow((((x2-x1)/x2)),2))< ERRO)
return x2;
else
return secante3( x1, x2, ERRO);
}
double haley1(double x0,double ERRO)
{
double x1;
x1=x0-((funcao1(x0)* derifuncao1(x0))/(pow(derifuncao1(x0),2)-(0.5*funcao1(x0)*derisecfuncao1(x0))));
if(sqrt(pow((((x1-x0)/x1)),2))< ERRO)
return x1;
else
return haley1( x1, ERRO);
}
double haley2(double x0,double ERRO)
{
double x1;
x1=x0-((funcao2(x0)* derifuncao2(x0))/(pow(derifuncao2(x0),2)-(0.5*funcao2(x0)*derisecfuncao2(x0))));
if(sqrt(pow((((x1-x0)/x1)),2))< ERRO)
return x1;
else
return haley2( x1, ERRO);
}
double haley3(double x0,double ERRO)
{
double x1;
x1=x0-((funcao3(x0)* derifuncao3(x0))/(pow(derifuncao3(x0),2)-(0.5*funcao3(x0)*derisecfuncao3(x0))));
if(sqrt(pow((((x1-x0)/x1)),2))< ERRO)
return x1;
else
return haley3( x1, ERRO);
}
int main(int argc, char *argv[])
{
double ERRO =pow(10,-15);
printf("menores raizes positivas Bisseccao\n");
printf(" x/2 -tag(x) =%lf\n ",bisseccao1(4, 5,ERRO));
printf("2*cos(x)-e^(x/2) = %lf\n ",bisseccao2(0, 1,ERRO));
printf(" x^5 -6 = %lf\n ",bisseccao3(1, 3,ERRO));
printf("menores raizes positivas newton\n");
printf(" x/2 -tag(x) =%lf\n ",newton1(4,ERRO));
printf("2*cos(x)-e^(x/2) = %lf\n ",newton2(0,ERRO));
printf(" x^5 -6 = %lf\n " ,newton3(1,ERRO));
printf("menores raizes positivas Secante\n");
printf(" x/2 -tag(x) =%lf\n ",secante1(3.9,4,ERRO));
printf("2*cos(x)-e^(x/2) = %lf\n ",secante2(0,1,ERRO));
printf(" x^5 -6 = %lf\n ",secante3(0.5,1,ERRO));
printf("menores raizes positivas haley\n");
printf(" x/2 -tag(x) =%lf\n ",haley1(4,ERRO));
printf("2*cos(x)-e^(x/2) = %lf\n ",haley2(0,ERRO));
printf(" x^5 -6 = %lf\n ",haley3(1,ERRO));
system("PAUSE");
return 0;
}
domingo, 19 de junho de 2011
Arvore AVL inserção e remocão
# include"stdio.h"
# include"stdlib.h"
struct avl
{
int info;
int bal;
struct avl *pai;
struct avl *esq;
struct avl *dir;
};
int avl_height ( struct avl *node )
{
/*retorna a altura da arvore*/
int esq, dir;
if ( node == NULL ) return -1;
esq = avl_height ( node->esq );
dir = avl_height ( node->dir );
if ( esq > dir )
return esq + 1;
else
return dir + 1;
}
struct avl *rotacaoLL(struct avl *p )
/* Rotação simples para a esquerda */
{
struct avl *q;
q = p->esq;
//----------------> Realiza a rotação
p->esq = q->dir;
if ( q->dir != NULL )
q->dir->pai = p;
q->dir = p;
q->pai = p->pai;
if ( p->pai != NULL )
{
if ( p->pai->esq == p )
p->pai->esq = q;
else
p->pai->dir = q;
}
p->pai = q;
//----------------> Rebalanceia
q->bal = avl_height ( q->esq ) - avl_height ( q->dir );
p->bal = avl_height ( p->esq ) - avl_height ( p->dir );
return q;
}
struct avl *rotacaoRR ( struct avl *p )
/* Rotação simples para a direita */
{
struct avl *q;
q = p->dir;
//----------------> Realiza a rotação
p->dir = q->esq;
if ( q->esq != NULL )
q->esq->pai = p;
q->esq = p;
q->pai = p->pai;
if ( p->pai != NULL )
{
if ( p->pai->esq == p )
p->pai->esq = q;
else
p->pai->dir = q;
}
p->pai = q;
//----------------> Rebalanceia
q->bal = avl_height ( q->esq ) - avl_height ( q->dir );
p->bal = avl_height ( p->esq ) - avl_height ( p->dir );
return q;
}
void avl_remove ( struct avl **node,int info )
{
int aux;
struct avl * P;
/*se se o elemento nao esta na arvore sai da funçao*/
if ( *node == NULL ) return ;
if ( info<(*node)->info )
{
/*se remove da esquerda é como inserir na direita*/
avl_remove ( &((*node)->esq), info );
(*node)->bal=avl_height ( (*node)->esq ) - avl_height ( (*node)->dir );
if( (*node)->bal==-2)
{
if( (*node)->dir->bal!=1)
{
(*node)= rotacaoRR ( (*node) );
}
else if( (*node)->dir->bal==1)
{
(*node)->dir=rotacaoLL ( (*node)->dir);
(*node)= rotacaoRR ( (*node) );
}
}
}
else
{
if ( info>(*node)->info )
{
/*se remove da direita é como inserir na esquerda*/
avl_remove ( &((*node)->dir), info );
(*node)->bal=avl_height ( (*node)->esq ) - avl_height ( (*node)->dir );
if( (*node)->bal==2)
{
if( (*node)->esq->bal!=-1)
{
(*node)= rotacaoLL ( (*node) );
}
else if( (*node)->esq->bal==-1)
{
(*node)->esq =rotacaoRR ( (*node)->esq );
(*node)= rotacaoLL ( (*node) );
}
}
}
else
{
/*se achou a informaçao na folha ela remove*/
if ( info==(*node)->info )
{
if ( ((*node)->esq==NULL)&&((*node)->dir==NULL ))
{
free ( *node );
*node = NULL;
}
else
{
/*se tive so filho direito ou lado direito mais alto troca com menor valor a direita ate chegar a folha remover do lado mais alto é muito importante */
if ( ((*node)->esq==NULL&&(*node)->dir!=NULL)||(*node)->bal==-1)
{
P=(*node)->dir;
while(P->esq!=NULL)
P=P->esq;
aux=P->info;
avl_remove ( &((*node)), P->info );
(*node)->info=aux;
}
else
{
/*se tiver so filho esquerdo ou os 2 filhos troca com menor valor a esquerda ate chegar a folha */
P=(*node)->esq;
while(P->dir!=NULL)
P=P->dir;
aux=P->info;
avl_remove ( &((*node)), P->info );
(*node)->info=aux;
}
}
}
}
}
}
void inserir(struct avl **pRaiz,struct avl **PAI, int numero)
{
if(*pRaiz == NULL)
{
* pRaiz = (struct avl *) malloc(sizeof(struct avl ));
(*pRaiz)->esq = NULL;
(*pRaiz)->dir = NULL;
(*pRaiz)->info = numero;
(*pRaiz)->pai=(*PAI);
(*pRaiz)->bal=0;
}
else
{
if(numero <(*pRaiz)->info)
{
inserir(& (*pRaiz)->esq,& (*pRaiz), numero);
(*pRaiz)->bal=avl_height ( (*pRaiz)->esq ) - avl_height ( (*pRaiz)->dir );
if( (*pRaiz)->bal==2)
{
if( (*pRaiz)->esq->bal==1)
(*pRaiz)= rotacaoLL ( (*pRaiz) );
else if( (*pRaiz)->esq->bal==-1)
{
(*pRaiz)->esq =rotacaoRR ( (*pRaiz)->esq );
(*pRaiz)= rotacaoLL ( (*pRaiz) );
}
}
}
else
{
if(numero >(*pRaiz)->info)
{
inserir(& (*pRaiz)->dir,& (*pRaiz), numero);
(*pRaiz)->bal=avl_height ( (*pRaiz)->esq ) - avl_height ( (*pRaiz)->dir );
if( (*pRaiz)->bal==-2)
{
if( (*pRaiz)->dir->bal==-1)
(*pRaiz)= rotacaoRR ( (*pRaiz) );
else if( (*pRaiz)->dir->bal==1)
{
(*pRaiz)->dir=rotacaoLL ( (*pRaiz)->dir);
(*pRaiz)= rotacaoRR ( (*pRaiz) );
}
}
}
}
}
}
/* Output function */
void Output(struct avl *Tree,int Level)
{
int i;
if (Tree)
{
Output(Tree->dir, Level+1);
printf("\n");
for (i = 0; i < Level; i++)
printf(" ");
printf("(%d)%d",Tree->bal, Tree->info);
Output(Tree->esq, Level+1);
}
}
void destruir(struct avl **Tree)
{
if ((*Tree)!=NULL)
{
destruir(&(*Tree)->dir);
destruir(&(*Tree)->esq);
free((*Tree));
(*Tree)=NULL;
}
}
/* Function main */
int main()
{
int op;
int valor ;
struct avl *L,*aux ;
L = aux= NULL;
do
{
printf("\nopcao\n1-insere\n2remove\n3destruir\n4-sair ");
scanf("%d", &op);
switch(op)
{
case 1:
for(int i=0; i<1000; i++)
inserir(&L,&aux,rand()%1000);
Output(L, 1);
fflush(stdin);
break;
case 2:
for(int i=0; i<1000; i++)
avl_remove ( &L, rand()%1000);
Output(L, 1);
break;
case 3:
destruir (&L);
Output(L, 1);
break;
}
}
while(op!=4);
return 0;
}
Assinar:
Comentários (Atom)