Last active
March 29, 2017 22:00
-
-
Save joanmolinas/33dd795be46ee05f76e7f2a13ea5f544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#define K 9000000000 | |
typedef struct { | |
double i; | |
double j; | |
} pair; | |
/** Funcions base */ | |
double modul(int x, int y) { | |
int x2 = pow(x, 2); | |
int y2 = pow(y, 2); | |
return sqrt(x2+y2); | |
} | |
pair camp_electric_vectorial(double Q, int x, int y) { | |
double m = pow(modul(x, y), 3); | |
double E = K * (Q/m); | |
double i = E * x; | |
double j = E * y; | |
pair p; | |
p.i = i; | |
p.j = j; | |
return p; | |
} | |
double potencial_electric(double Q, double distancia_xy) { | |
return K*(Q/distancia_xy); | |
} | |
/** Camp elèctric */ | |
void camp_electric() { | |
int nCarregues; | |
printf("Nº de càrregues :\n"); | |
scanf("%d", &nCarregues); | |
double acumI = 0, acumJ = 0; | |
for (int i = 0; i < nCarregues; i++) { | |
double Q; | |
int x, y, exp; | |
printf("Càrrega (Q, 10^exp) :\n"); | |
scanf("%lf %d", &Q, &exp); | |
printf("(X, Y) :\n"); | |
scanf("%d %d", &x, &y); | |
Q = Q * pow(10, exp); | |
pair p = camp_electric_vectorial(Q, x, y); | |
printf("%lf %lf \n", p.i, p.j); | |
acumI += p.i; | |
acumJ += p.j; | |
} | |
printf("%lf %lf \n", acumI, acumJ); | |
} | |
/** Energia potencial sistema */ | |
double energia_potencial_una_carrega(double Q, int nCarregues) { | |
double acum = 0; | |
for (int i = 0; i < nCarregues; i++) { | |
double distancia; | |
printf("Distancia:\n"); | |
scanf("%lf", &distancia); | |
acum += potencial_electric(Q, distancia); | |
} | |
return Q*acum; | |
} | |
void energia_potencial_sistema() { | |
int nCarregues; | |
double Q; | |
int exp; | |
printf("Nº de càrregues :\n"); | |
scanf("%d", &nCarregues); | |
printf("Càrrega (Q, 10^exp) :\n"); | |
scanf("%lf %d", &Q, &exp); | |
Q = Q * pow(10, exp); | |
double sum = 0; | |
for (int i = 0; i < nCarregues; i++) { | |
printf("------- Q%d -------\n",i+1); | |
double epq = energia_potencial_una_carrega(Q, nCarregues-1); | |
sum += epq; | |
printf("%lf \n", epq); | |
printf("%lf \n", sum); | |
} | |
printf("Ep = %lf J \n", 0.5*sum); | |
} | |
/** Potencial elèctric sistema de càrregues */ | |
double potencial_carregues_sistema_un_punt(double *sistema, int nCarregues, pair *coordenades, pair punt) { | |
double acum = 0; | |
for (int i = 0; i < nCarregues; i++) { | |
int cx = coordenades[i].i; | |
int cy = coordenades[i].j; | |
int x = punt.i - cx; | |
int y = punt.j - cy; | |
double m = modul(x, y); | |
acum += potencial_electric(sistema[i], m); | |
} | |
return acum; | |
} | |
void potencial_electric_sistema() { | |
int nCarregues; | |
printf("Nº de càrregues :\n"); | |
scanf("%d", &nCarregues); | |
double sistema[nCarregues]; | |
pair coordenades[nCarregues]; | |
for (int i = 0; i < nCarregues; i++) { | |
double Q; | |
int exp; | |
printf("Càrrega %d (Q, 10^exp) :\n", i+1); | |
scanf("%lf %d", &Q, &exp); | |
sistema[i] = Q*pow(10, exp); | |
double x, y; | |
printf("Punt %d (x, y) :\n", i+1); | |
scanf("%lf %lf", &x, &y); | |
coordenades[i].i = x; | |
coordenades[i].j = y; | |
} | |
int nPunts; | |
printf("Nº de Punts:\n"); | |
scanf("%d", &nPunts); | |
for (int i = 0; i < nPunts; i++) { | |
double x, y; | |
printf("Punt %d (x, y) :\n", i+1); | |
scanf("%lf %lf", &x, &y); | |
pair punt; | |
punt.i = x; | |
punt.j = y; | |
printf("V(p%d) = %f V\n", i+1, potencial_carregues_sistema_un_punt(sistema, nCarregues, coordenades, punt)); | |
} | |
} | |
int main(int argc, const char * argv[]) { | |
int opcio; | |
do { | |
printf("1) Calcular camp elèctric creat per càrregues en un punt.\n"); | |
printf("2) Calcular energia potencial del sistema amb totes càrregues iguals.\n"); | |
printf("3) Potencial elèctric (origen potencial ∞) d'un sistema de càrregues\n"); | |
printf("Opció: \n"); | |
scanf("%d", &opcio); | |
switch (opcio) { | |
case 1: | |
camp_electric(); | |
break; | |
case 2: | |
energia_potencial_sistema(); | |
break; | |
case 3: | |
potencial_electric_sistema(); | |
break; | |
default: | |
break; | |
} | |
} while (1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment