segunda-feira, 23 de março de 2009

Métodos Numéricos

Pessoal tive hoje teste de métodos e saiu-me um exercicio de Bissecção Sucessiva e outro de Newton.
Deixo-vos com a minha resolução, que como é obvio está correcta, aqui em baixo.
Divirtam-se e não se esqueçam de mudar algumas merdas senão ainda me fodo.

Os includes são stdio.h e math.h. Esta merda não tava a dar direito


----------------------------------------------------------------------------------
BISSECÇÃO SUCESSIVA
----------------------------------------------------------------------------------
#include
#include

float f(float x)
{
return (0.35*x*x*x-2.1*x+1-2*sin(x));
}

void bs(float a, float b,float erro)
{
int n;
float x;
float error=100;
printf("n\tan\t\tf(an)\t\tbn\t\tf(bn)\t\tx(n+1)\t\tf(x(n+1))\terro\n");
while(error>erro)
{
x=(a+b)/2;
error=x-a;
printf("%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",n,a,f(a),b,f(b),x,f(x),error);
if(f(x)*f(a)<0) b=x; else a=x; n++;
}
}

main()
{
float x,a=2,b=3;
float erro=0.005; bs(a,b,erro);
}

----------------------------------------------------------------------------------
NEWTON

----------------------------------------------------------------------------------
#include
#include

double d(double x)
{
return (1.05*x*x-2.1-2*cos(x));
}

double dd(double x)
{
return (2.1*x+2*sin(x));
}

double f(double x)
{
return (0.35*x*x*x-2.1*x+1-2*sin(x));
}

void newton (double a,double b,double erro)
{
int c;
double x1=2.496094,x2,error;
error = 1;
printf("n\tx\t\tf(x)\t\tf'(x)\t\tx(n+1)\t\terro\n");
while(error>erro)
{
x2= x1-(f(x1)/d(x1));
error= fabs((x2-x1));
printf("%d\t%e\t%e\t%e\t%10.8f\t%e\n",c,x1,f(x1),d(x1),x2,error);
c++;
x1=x2;
}
}

main()
{
double a=2,b=3,erro=0.00000001;
newton(a,b,erro);
}
----------------------------------------------------------------------------------

6 comentários:

  1. lol a fazer concorrência aos agarrados?

    ResponderEliminar
  2. fds inda bem k n tenho exa caca

    ResponderEliminar
  3. sou professor de metodos e estao todos reprovados...

    ResponderEliminar
  4. È assim mesmo ó CMM! Reprova esses gajos!

    ResponderEliminar
  5. TIMONEIRO a salvar o dia!!! quer dizer so salvas-te meio dia k o outro exercício era diferente...

    ResponderEliminar