Skip to content

Instantly share code, notes, and snippets.

@ScratchyCode
Last active September 28, 2022 22:21
Show Gist options
  • Save ScratchyCode/70a60fcc70ba94333b83bd093b45ac41 to your computer and use it in GitHub Desktop.
Save ScratchyCode/70a60fcc70ba94333b83bd093b45ac41 to your computer and use it in GitHub Desktop.
Plotting complex functions. To launch run in terminal: bash start.sh
// Coded by Scratchy
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
#include <tgmath.h>
//#define EPSILON 0.0001
void polari(long double a, long double b, long double *rho, long double *theta);
void cartesiane(long double rho, long double theta, long double *a, long double *b);
double complex funzione(long double complex z){
return clogl(z);
}
int main(){
long double complex z, w;
long double az, bz, rhoz, thetaz, aw, bw, rhow, thetaw, rhozmax, thetazmax, inc;
unsigned long long int t=0;
printf("Inserisci il raggio rho massimo: ");
int foo1 = scanf("%Lf",&rhozmax);
printf("Inserisci il numero di giri dell'angolo theta: ");
int foo2 = scanf("%Lf",&thetazmax);
printf("Inserisci l'incremento per la definizione del grafico: ");
int foo3 = scanf("%Lf",&inc);
// file immagine
FILE *pf = fopen("plot.dat","w");
if(pf == NULL){
perror("\n");
exit(1);
}
fprintf(pf,"# i real(z) arg(z) real(w) arg(w) rhoz thetaZ rhoW thetaW abs(w)\n");
/*
Superfici di Riemann con gnuplot:
2:3:4 <- parte reale
2:3:5 <- parte immaginaria
*/
// punto iniziale
rhoz = 0.;
thetaz = 0.;
printf("Calcolo...\n");
do{
do{
cartesiane(rhoz,thetaz,&az,&bz);
z = az + bz * I;
w = funzione(z);
polari(creal(w),cimag(w),&rhow,&thetaw);
fprintf(pf,"%llu\t%Lf\t%Lf\t%Lf\t%Lf\t%Lf\t%Lf\t%Lf\t%Lf\t%lf\n",t,creal(z),cimag(z),creal(w),cimag(w),rhoz,thetaz,rhow,thetaw,cabs(w));
thetaz += inc;
t++;
}while(thetaz <= (2*M_PI)*thetazmax);
thetaz = 0.; // reset dell'angolo
rhoz += inc; // incremento il raggio
}while(rhoz <= rhozmax);
return 0;
}
void polari(long double a, long double b, long double *rho, long double *theta){
*rho = sqrt((a*a)+(b*b));
*theta = atan(b/a);
return ;
}
void cartesiane(long double rho, long double theta, long double *a, long double *b){
*a = rho * cos(theta);
*b = rho * sin(theta);
return ;
}
import cplot
import numpy as np
# funzione
def f(z):
return np.sin(1/z**(np.sqrt(z)))
# plot
n = 5.0
#plt = cplot.plot(f,(-4.0,4.0,600),(-4.0,4.0,600))
plt = cplot.plot(
f,
(-n, +n, 1000),
(-n, +n, 1000),
#abs_scaling = lambda x: x / (x + 1), # how to scale the lightness in domain coloring
#contours_abs = 2.0,
#contours_arg = (-np.pi / 2, 0, np.pi / 2, np.pi),
#emphasize_abs_contour_1 = True,
#add_colorbars = True,
#add_axes_labels = True,
#saturation_adjustment = 1.28,
#min_contour_length = None,
)
plt.show()
# sfera di riemann
plt = cplot.riemann_sphere(f)
plt.show()
import pylab
import matplotlib.pyplot as plt
# input dati
#nome_file = input("Inserisci nome file dati: ") # nel caso si usi python3
nome_file = "plot.dat"
# lettura dati
t,x,iy,w,iw,rhoz,thetaz,rhow,thetaw,absw = pylab.loadtxt(nome_file,unpack=True)
# Superficie di Riemann - parte reale
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(projection='3d')
sequence_containing_x_vals = x
sequence_containing_y_vals = iy
sequence_containing_z_vals = w
ax.scatter(sequence_containing_x_vals,sequence_containing_y_vals,sequence_containing_z_vals,c='b',marker='.',s=0.1)
ax.set_xlabel('x')
ax.set_ylabel('iy')
ax.set_zlabel('Re(w)')
plt.show()
# Superficie di Riemann - parte immaginaria
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(projection='3d')
sequence_containing_x_vals = x
sequence_containing_y_vals = iy
sequence_containing_z_vals = iw
ax.scatter(sequence_containing_x_vals,sequence_containing_y_vals,sequence_containing_z_vals,c='r',marker='.',s=0.1)
ax.set_xlabel('x')
ax.set_ylabel('iy')
ax.set_zlabel('Im(w)')
plt.show()
# Superficie modulare
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(projection='3d')
sequence_containing_x_vals = x
sequence_containing_y_vals = iy
sequence_containing_z_vals = absw
color = thetaw
punti = ax.scatter(sequence_containing_x_vals,sequence_containing_y_vals,sequence_containing_z_vals,c=color,marker='.',s=0.1,cmap="jet")
ax.set_xlabel('x')
ax.set_ylabel('iy')
ax.set_zlabel('Abs(w)')
cbar = plt.colorbar(punti)
cbar.set_label("theta")
plt.show()
#!/bin/bash
gcc cPlot.c -lm -O3 -o cPlot.exe
./cPlot.exe
echo "Plotting..."
#gnuplot script.gp
python3 script.py
echo "Fine."
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment