Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main(){
  6. int i,j,a,tamanho_sist,max_inte;
  7. double erro_max;
  8. FILE *file;
  9. char nome_arq[60];
  10.  
  11.  
  12. do{
  13. printf("*****---SEJA BEM VINDO---*****\n\n\n\n\n");
  14. printf("Digite o diretorio do arquivo(inclua o .txt):\n");
  15. scanf("%[^\n]s",&nome_arq);
  16. fflush(stdin);
  17.  
  18. file = fopen(nome_arq,"r");
  19. if(file == NULL){
  20. printf("\nErro ao abrir o arquivo, tente novamente \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  21. system("pause");
  22. }
  23. system("cls");
  24. }while(file == NULL);
  25.  
  26. fscanf(file,"%i %i %lf",&tamanho_sist,&max_inte,&erro_max);
  27.  
  28. double *pont = (double *) calloc(tamanho_sist *(tamanho_sist+ 1), sizeof(double));
  29. double matriz[tamanho_sist][tamanho_sist + 1];
  30.  
  31. //********** Funções declaradas após pegado os dados do arquivo***********----------------------------------------------------------------
  32. double jacobi(int n,double matriz[][n + 1],int max_inte,double erro_max){
  33. int i,j,contador,cont_erro;
  34. double konst,x[n],y[n];
  35. contador = 0;
  36. for(i=0;i<n;i++){
  37. x[i] = 0;
  38. y[i] = x[i];
  39. }
  40.  
  41. do{
  42. cont_erro = 0;
  43.  
  44. printf("\n\ninteracao no %i\t",contador + 1);
  45. for(i=0;i<n;i++){
  46. printf(" \tx(%i) = %.2lf\t",i+1,x[i]);
  47. }
  48.  
  49. for(i=0;i<n;i++){
  50. konst = 0.0;
  51. for(j=0;j<n;j++){
  52. if(i != j){
  53. konst = konst + y[j]*matriz[i][j];
  54. }
  55. }
  56. x[i] = (matriz[i][n] - konst)/matriz[i][i];
  57. }
  58.  
  59. for(i=0;i<n;i++){
  60. if(fabs(x[i]-y[i]) < erro_max){
  61. cont_erro++;
  62. }
  63. y[i] = x[i];
  64. }
  65.  
  66.  
  67.  
  68.  
  69. contador++;
  70. if(contador == max_inte){
  71. printf("\n\n\n\t**Maxima interacao atingida!**\n");
  72. break;
  73. }
  74. }while(cont_erro != n);
  75.  
  76. if(cont_erro == n){
  77. printf("\n\n\t**Minimo erro atingido!**\n");
  78. }
  79. printf("\n\n\n\tValores encontrados:\n");
  80.  
  81. for(i=0;i<n;i++){
  82. printf("\tx[%i] = %.2lf",i+1,x[i]);
  83. }
  84. printf("\n\n\n\n\n\n\n");
  85. }
  86.  
  87. //********** Fim das declarações de funções***********------------------------------------------------------------------------------------
  88.  
  89. printf("COonsiderando: tamanho do sistem = %i max intera = %i erromax = %lf\n",tamanho_sist,max_inte,erro_max);
  90. printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
  91.  
  92. for(i=0;i<tamanho_sist;i++){
  93. for(j=0;j<tamanho_sist + 1;j++){
  94. fscanf(file,"%lf",&matriz[i][j]);
  95. }
  96. }
  97. fclose(file);
  98. system("pause");
  99.  
  100. while(1){
  101. do{
  102. system("cls");
  103. printf("\nPara a sua matriz: \n");
  104. for(i=0;i<tamanho_sist;i++){
  105. for(j=0;j<tamanho_sist+1;j++){
  106. printf("\t%.1lf",matriz[i][j]);
  107. }
  108. printf("\n");
  109. }
  110. printf("\nEscolha uma operacao:\n");
  111. printf("1 *- Calcular pelo Metodo de Gauss-Jordan com pivotacao parcial.\n2 *- Calcular pelo Metodo de Gauss-Jordan sem pivotacao.\n3 *- Metodo iterativo de Jordan com dominancia por linha.\n4 *- Metodo iterativo de Seidel com dominancia por linha.\n5 *- Sair.\n");
  112. scanf("%i",&a);
  113. fflush(stdin);
  114. }while(a>5 || a<1);
  115. switch(a){
  116. case 1:
  117. system("cls");
  118. printf("\nvai pivotar e gauss-jordar\n");
  119. //1- FUNÇÃO DE PIVOTAÇÃO PARCIAL 2- FUNÇÃO Q CALCULA GAUSS-JORDAN
  120. system("pause");
  121. break;
  122. case 2:
  123. system("cls");
  124. printf("\nvai gaauss-jordar\n");
  125. //1- FUNÇÃO Q CALCULA GAUSS JORDAN
  126. system("pause");
  127. break;
  128. case 3:
  129. system("cls");
  130. printf("\nvai jordar\n");
  131. jacobi(tamanho_sist,matriz,max_inte,erro_max);
  132. system("pause");
  133. //1- FUNÇÃO DE PIVOTAÇÃO PARCIAL 2- FUNÇÃO Q CALCULA INT.JORDAN
  134. break;
  135. case 4:
  136. system("cls");
  137. printf("\nvai seidar \n");
  138. system("pause");
  139. //1- FUNÇÃO DE PIVOTAÇÃO PARCIAL 2- FUNÇÃO Q CALCULA PELO METODO INTERATIVO DE SEIDEL
  140. break;
  141. case 5:
  142. system("cls");
  143. return 0;
  144. break;
  145. default:
  146. printf("\nDigite um numero valido...\n");
  147. break;
  148.  
  149.  
  150. }
  151. }
  152.  
  153.  
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement