Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Temas - fernando27

#1
Programación C/C++ / problemas en el caso 2
29 Noviembre 2015, 02:58 AM
buenas, alguien podria ayudarme a solucionar un inconveniente que tengo al momento de querer obtener los valores del caso 2(los del caso 1 si se obtienen). el problema se puede ejecutar... gracias...
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#define Pi 3.141592653589
using namespace  std;
///funcion u(x,t) aproximada
long double U(double x, double t)
{
long double z; int i;
z=0;
for (i=1;i<20;i++)
z = z+2/(Pi*Pi)*1/(i*i)*(4*sin(Pi*i/2)+i*Pi*(1-cos(i*Pi)))*sin(i*Pi*x)*exp(-Pi*Pi*t*i*i);
return(z);} ///////fin funcion aproximada
int main ()
{  int opc;
   float y,q;
do { system("cls");
   cout<<"menu\n";
   cout<<"1. Metodo Explicito\n";
   cout<<"2. MEtodo Implicito\n";
   cout<<"\t\t elija su opcion:";
   cin>> opc;
switch(opc)
{case 1:{system("cls");
       cout<<"Metodo Explicito\n";
       cout<<"\t\t digite un valor para r :";
       cin>>y;
       FILE *f;
f=fopen ("explicito020.txt","w+");
int i,j,N,T,l,c;
float s1,s2,s3,r,h,k,x,t,error,diferencia;
float zz;
float u[11][101];
r=y;
h=0.1;
k=r*h*h;
N=10;
T=50;
fprintf(f,"r=%4.2f,h=%4.2f,k=%6.4f \n ",r,h,k);
fprintf(f,"i j x t U[xi,yj] U[x,y] diferencia error \n ");
printf("i j x t U[xi,yj] U[x,y] diferencia error \n ");
for (l=0;l<=N+1;l++) //valores de U con las condiciones iniciales{
x=l*h;
if ((x>=0 ) & (x<= 0.5)){
u[l][0]=2*x+1;
fprintf(f,"%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n ",l,0,x,0.0,u[l][0],u[l][0],0.0,0.0);
printf("%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",l,0,x,0.0,u[l][0],u[l][0],0.0,0.0);
}//fin del if
if ((x>0.5 ) & (x<=1)){
u[l][0]=3-2*x;
fprintf(f,"%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n ",l,0,x,0.0,u[l][0],u[l][0],0.0,0.0);
printf("%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",l,0,x,0.0,u[l][0],u[l][0],0.0,0.0);
} //fin del if
} //fin del for
fprintf(f,"%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",l,0,x,0.0,u[l][0],u[l][0],0.0,0.0);
printf("%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",l,0,x,0.0,u[l][0],u[l][0],0.0,0.0);
for(j=0;j<=100;j++){
u[0][j]=0.2;
u[10][j]=0.4;
t=(j+1)*k;
for (i=0;i<N+1;i++){
if ((i==0) || (i==10))
{x=i*h;
fprintf(f,"%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",i,j+1,x,t,u[j],u[j],0.0,0.0);
printf("%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",i,j+1,x,t,u[j],u[j],0.0,0.0);}
else{
x=i*h;
t=(j+1)*k;
s1=u[i-1][j];
s2=u[j];
s3=u[i+1][j];
u[j+1]=r*s1+(1-2*r)*s2+r*s3;
zz=U(x,t);
diferencia=fabs(zz-u[j+1]);
error=fabs((u[j+1]-zz)/zz)*100;
fprintf(f,"%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",i,j+1,x,t,u[j+1],zz,diferencia,error);
printf("%d %d %5.3f %5.3f %6.4f %6.4f %6.4f %6.4f \n",i,j+1,x,t,u[j+1],zz,diferencia,error);}} // fin del for i}//fin del for j
fclose(f);
printf("fin del programa");
while((c = getchar()) != '\n' && c != EOF);
   putchar( c );
break;
}case 2:{system("cls");
       cout<<"Metodo Implicito\n";
       cout<<"\t\t digite un valor para r :";
       cin>>q;
        int i,j,c,l,s;
FILE *f1,*f;
int N=5;
float r=q;
float h=0.1;
float k=r*h*h;
float a[20][20],b[20][20],A[20],B[20],x,xx,diferencia,error,t,zzz;
f=fopen("implicito070.txt","w+");
fprintf(f1,"PARA EL CASO IMPLICITO CRANCK NICHOLSON BVP \n");
fprintf(f1,"r=%3.2f, h=%3.2f,k=%5.3f \n",r,h,k);
fprintf(f1," i j x t U[i,j] Uanalitica diferencia error \n");
printf("PARA EL CASO IMPLICITO CRANCK NICHOLSON BVP r=%3.2f, h=%3.2f\n",r,h);
printf(" i j x t U[i,j] Uanalitica diferencia error \n");
for (i=0;i<=2*N+1;i++) {
x=i*h;
if ((x>=0 ) & (x<=0.5)) {
a
  • =2*x+1;
       a[0][j]=0.2;
    a[10][j]=0.4;
    t=(j+1)*k;
    a[10-i][0]=a
    • ;
      zzz=a
      • ;
        diferencia=fabs(zzz-a
        • );
          error=0;
          fprintf(f1," %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,0,x,0.0,a
          • ,zzz,diferencia,error);
            printf(" %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,0,x,0.0,a
            • ,zzz,diferencia,error);}//fin del if
              if ((x>0.5 ) & (x<1)){
                 a
              • =3-2*x;
                   a[0][j]=0.2;
                a[10][j]=0.4;
                t=(j+1)*k;
                zzz=a[10-i][0];
                diferencia=fabs(zzz-a
                • );
                  error=diferencia/zzz*100;
                  fprintf(f1," %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,0,x,0.0,a
                  • ,zzz,diferencia,error);
                    printf(" %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,0,x,0.0,a
                    • ,zzz,diferencia,error);}//fin del if
                      if (x==1){
                      zzz=a[10-i][0];
                      diferencia=fabs(zzz-a
                      • );
                        error=0;
                        fprintf(f1," %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,0,x,0.0,a
                        • ,zzz,diferencia,error);
                          printf(" %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,0,x,0.0,a
                          • ,zzz,diferencia,error);}}///fin del for i para llenarlos valores iniciales
                            for (j=0; j<20;j++) {
                            for (i=1;i<N+1;i++) {
                               if (i==1) {
                            b=4.0;
                            b[i+1]=-1;
                            ///rellena de ceros el lado derecho
                            for (l=i+2;l<N+1;l++){
                            b[l]=0.0;}
                            b[N+1]= (2-2*r)*a[j]+r*a[i+1][j];
                            printf("\n");
                            fprintf(f,"\n");
                            }
                            if ((i>1) && (i!=N)) {
                            if(i>2) {
                            ///rellena de ceros el lado izquierdo
                            for (l=1;l<i-1;l++){
                            b[l]=0.0;}}
                            b[i-1]=-1;
                            b=4.0;
                            b[i+1]=-1;
                            for (l=i+2;l<N+1;l++){
                            b[l]=0.0;}
                            b[N+1]= r*a[i-1][j]+(2-2*r)*a[j]+r*a[i+1][j];
                            }//fin del if i>1
                            if (i==N){
                            for (l=1;l<i-1;l++){
                            b[l]=0.0;}
                            b[N-1]=-2.0*r;
                            b[N]=(2+2*r);
                            b[N+1]= r*a[i-1][j]+(2-2*r)*a[j]+r*a[i+1][j];
                            //a[i-1][j]+a[i+1][j];
                            printf("\n");
                            } }///fin del for I=1,N
                            printf("\n");
                            ////METODO GAUSS SIN PIVOTEO-METOD PROGONKA////
                            ///apli-camos progonka directa///
                            A[1]=-b[1][2]/b[1][1];
                            B[1]=b[1][N+1]/b[1][1];
                            for (i=2;i<N+1;i++){
                            if (i!= N){
                            A=-b[i+1]/(b+b[i-1]*A[i-1]);}
                            B=(b[N+1]-b[i-1]*B[i-1])/(b+b[i-1]*A[i-1]);}
                            //ahora hallamos las soluciones
                            //printf("\n");
                            xx=B[N];
                            //las soluciones son los elementos del vector a[j] a[N][j+1]=xx;
                            printf( "a[%d][%d]= %6.4f \n",N,j+1,a[N][j+1]);
                            l=N;
                            while (l>0) {
                            s=10-l;
                            xx=A[l]*xx+B[l];
                            a[l][j+1]=xx;
                            a[j+1]=xx;
                            fprintf(f1, "a[%d][%d]= %6.4f \n",l,j+1,a[j+1]);
                            printf( "a[%d][%d]= %6.4f \n",l,j+1,a[l][j+1]);
                            l=l-1;}
                            printf("\n");
                            t=k*(j+1);
                            for (i=0;i<2*N+1;i++) //ahora escribe las soluciones en el archivo de texto{
                            x=i*h;
                            if ((i==0) || (i==2*N)){
                            printf(" %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,j+1,x,t,0.0,0.0,0.0,0.0);
                            fprintf(f1," %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,j+1,x,t,0.0,0.0,0.0,0.0);}
                            else {
                            zzz=U(x,t);
                            diferencia=fabs(zzz-a[j+1]);
                            error=diferencia/zzz*100;
                            fprintf(f1," %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,j+1,x,t,a[j+1],zzz,diferencia,error);
                            printf(" %d %d %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f \n", i,j+1,x,t,a[j+1],zzz,diferencia,error);}}}//fin del j principal fclose(f);
                            fclose(f1);
                            while((c = getchar()) != '\n' && c != EOF);
                               break;}};
                            }while(opc<=2);
                            }