0
votes

I have an question for my plot. I want to plot some functions in a range but I don't know how to define the x axis because there a different variables. Here is my plot command for gnuplot:

reset
set grid
#set autoscale xy
set yrange [0:150]
set xrange [0:100]
set ytics 10 nomirror

sig = abs(1 - (x1 * x2 / xh**2))
print sig

set dummy s

p = 2

fn = 50.0
f1 = 10.0
f2 = 25.0
f3 = 35.0
f5 = 60.0
f6 = 75.0
f7 = 100.0

un = 400.0
u1 = f1/fn * un
u2 = f2/fn * un
u3 = f3/fn * un
u5 = un
u6 = un
u7 = un

ns1 = f1 / p
ns2 = f2 / p
ns3 = f3 / p
ns4 = fn / p
ns5 = f5 / p
ns6 = f6 / p
ns7 = f7 / p

skn = r2 / (sig * x2)
sk1 = skn * fn / f1
sk2 = skn * fn / f2
sk3 = skn * fn / f3
sk5 = skn * fn / f5
sk6 = skn * fn / f6
sk7 = skn * fn / f7

s1(s) = s * sk1
s2(s) = s * sk2
s3(s) = s * sk3
s4(s) = s * skn
s5(s) = s * sk5
s6(s) = s * sk6
s7(s) = s * sk7

n1(s) = ns1 * (1-s1(s))
n2(s) = ns2 * (1-s2(s))
n3(s) = ns3 * (1-s3(s))
n4(s) = ns4 * (1-s4(s))
n5(s) = ns5 * (1-s5(s))
n6(s) = ns6 * (1-s6(s))
n7(s) = ns7 * (1-s7(s))


mkn = 3 * un**2 * (1 - sig) / (4 * pi * fn/p * sig * x1)
mk1 = mkn 
mk2 = mkn
mk3 = mkn
mk5 = mkn * (u5 / un * fn / f5)**2
mk6 = mkn * (u6 / un * fn / f6)**2
mk7 = mkn * (u7 / un * fn / f7)**2

print("MkN")
print mkn
print mk1
print mk2
print mk3
print mk5
print mk6
print mk7

mi1(s)  = mkn * 2 / (s1(s) / sk1 + sk1 / s1(s))
mi2(s)  = mkn * 2 / (s2(s) / sk2 + sk2 / s2(s))
mi3(s)  = mkn * 2 / (s3(s) / sk3 + sk3 / s3(s))
mi4(s)  = mkn * 2 / (s4(s) / skn + skn / s4(s))
mi5(s)  = mkn * 2 / (s5(s) / sk5 + sk5 / s5(s))
mi6(s)  = mkn * 2 / (s6(s) / sk6 + sk6 / s6(s))
mi7(s)  = mkn * 2 / (s7(s) / sk7 + sk7 / s7(s))

set samples 10E3
plot    mi1(s) / (0<=s) / (s<=1.5) w l lw 1.5 lc "blue" title " M_{i10Hz}"

It don't look like what it should look like. We have an equal plot command for mathlab that works. Here it is:

clear all;
clc;


R2=1;

X1sigma=5;
X2sigma=3;
Xh=45;

X1=X1sigma+Xh;
X2=X2sigma+Xh;



sig=abs(1- (X1*X2)/(abs(Xh^2)));


%frequenzen
f1n=50;
f1 = 10;
f2 = 25;
f3 = 35;
f4 = 50;
f5 = 60;
f6 = 75;
f7 = 100;

%Synchrondrehzahlen
ns=25;
ns1 = 5;
ns2 = 12.5;
ns3 = 17.5;
ns4 = 25;
ns5 = 30;
ns6 = 37.5;
ns7 = 50;

%Kippschlupf
Skn=R2 / (sig * X2);
Sk1= Skn * f1n / f1;
Sk2= Skn * f1n / f2;
Sk3= Skn * f1n / f3;
Sk4= Skn * f1n / f4;
Sk5= Skn * f1n / f5;
Sk6= Skn * f1n / f6;
Sk7= Skn * f1n / f7;

%Schlupf
S1= 0:0.01:1.5 *Sk1;
S2= 0:0.01:1.5 *Sk2;
S3= 0:0.01:1.5 *Sk3;
S4= 0:0.01:1.5 *Sk4;
S5= 0:0.01:1.5 *Sk5;
S6= 0:0.01:1.5 *Sk6;
S7= 0:0.01:1.5 *Sk7;

%Spannungen 
U1N=400;
U1=U1N/5;
U2=U1N/2;
U3=U1N/1.43;
U4=U1N;
U5=U1N;
U6=U1N;
U7=U1N;

%Drehzahlen
n1= ns1 .* ( 1 - S1 );
n2= ns2 .* ( 1 - S2 );
n3= ns3 .* ( 1 - S3 );
n4= ns4 .* ( 1 - S4 );
n5= ns5 .* ( 1 - S5 );
n6= ns6 .* ( 1 - S6 );
n7= ns7 .* ( 1 - S7 );

%Kippmomente
Mkn= (3*(U1N^2)*(1-sig)) / (4 * pi * ns * sig * X1 );
Mk1=Mkn*(((U1/U1N)*(f1n/f1))^2);
Mk2=Mkn*(((U2/U1N)*(f1n/f2))^2);
Mk3=Mkn*(((U3/U1N)*(f1n/f3))^2);
Mk4=Mkn*(((U4/U1N)*(f1n/f4))^2);
Mk5=Mkn*(((U5/U1N)*(f1n/f5))^2);
Mk6=Mkn*(((U6/U1N)*(f1n/f6))^2);
Mk7=Mkn*(((U7/U1N)*(f1n/f7))^2);

%Drehmomente
Mn=33.705;
Mi1=(Mk1 .* 2.0 ) ./ ( ( S1 ./ Sk1 ) + ( Sk1 ./ S1 ) );
Mi2=(Mk2 .* 2.0 ) ./ ( ( S2 ./ Sk2 ) + ( Sk2 ./ S2 ) );
Mi3=(Mk3 .* 2.0 ) ./ ( ( S3 ./ Sk3 ) + ( Sk3 ./ S3 ) );
Mi4=(Mk4 .* 2.0 ) ./ ( ( S4 ./ Sk4 ) + ( Sk4 ./ S4 ) );
Mi5=(Mk5 .* 2.0 ) ./ ( ( S5 ./ Sk5 ) + ( Sk5 ./ S5 ) );
Mi6=(Mk6 .* 2.0 ) ./ ( ( S6 ./ Sk6 ) + ( Sk6 ./ S6 ) );
Mi7=(Mk7 .* 2.0 ) ./ ( ( S7 ./ Sk7 ) + ( Sk7 ./ S7 ) );


%plots
plot(2*n1, Mi1,'LineWidth', 2);
hold on;

plot(2*n2, Mi2,'LineWidth', 2);
hold on;

plot(2*n3, Mi3,'LineWidth', 2);
hold on;

plot(2*n4, Mi4,'LineWidth', 2);
hold on;

plot(2*n5, Mi5,'LineWidth', 2);
hold on;

plot(2*n6, Mi6,'LineWidth', 2);
hold on;

plot(2*n7, Mi7,'LineWidth', 2);
hold on;

axis([0 100 0 150]);

xlabel("n/1/s");
ylabel("M/Nm");

title("")
%Legende
hl = legend("10Hz", "25Hz", "35Hz", "50Hz", "60Hz", "75HZ", "100Hz" );

set(hl,'Interpreter','latex','Location', 'Northeast','FontSize',12);

grid on;
grid minor;

And here it is what it should look like. Plot

Maybe someone can help me. Thanks.

1
what should x2 be? gnuplot complains: undefined variable: x2. What should xh be? Hence sig is undefined etc... - theozh
I don't know Matlab.... but for gnuplot keep in mind when defining variables that it does integer division, e.g. a=2/3 and print a returns 0. Only a=2./3 or a= 2/3. will return 0.66666. - theozh
Welcome on Stackoverflow.com. Please reduce your sample code as minimal. stackoverflow.com/help/minimal-reproducible-example - Tom Solid
Several definitions are not equal in your gnuplot and your Matlab script, e.g. gnuplot mk1=mkn, but in Matlab Mk1=Mkn*(((U1/U1N)*(f1n/f1))^2). Furthermore, you plot only m1(s) in the gnuplot script. So it can't give the same plot result. Please check and edit your gnuplot code. - theozh
that was my mistake. I've copied this part for testing in a separated plot. but it doesn't matter the result was the same. And I know about the problem with integer and float division. But thanks for your help. - iPhil

1 Answers

2
votes

As mentioned in the comments, your gnuplot script does not match your Matlab script in several definitions.

Especially in your case, I would strongly recommend to define arrays and functions. This will avoid copy paste mistakes and will make the code shorter and clearer. I hope you are working with gnuplot >=5.2, because the code below uses arrays.

I started from your Matlab script and cleaned it up a bit. Having a semicolon ; at the end of a line in gnuplot is not necessary, but it also doesn't harm. The power in Matlab ^ is ** in gnuplot. By the way, in your Matlab code I can't see where the variable Mn=33.705 is used. Furthermore, as mentioned in the comments, pay attention when using divisions in gnuplot, because gnuplot is using integer division if not one of the numbers is a float number. This is one of the meanest pitfalls in gnuplot getting unexpected results. For the colors you could also use RGB hexadecimal-numbers in the format 0xRRGGBB.

Code:

### plotting using arrays and functions
reset session

R2=1;
X1sigma=5.0;
X2sigma=3.0;
Xh=45.0;
X1=X1sigma+Xh;
X2=X2sigma+Xh;
sig=abs(1- (X1*X2)/(abs(Xh**2)));

#%frequenzen
f1n=50.0;
array fArr[7] = [10., 25., 35., 50., 60., 75., 100.]

#%Synchrondrehzahlen
ns=25;
array nsArr[7] = [5., 12.5, 17.5, 25., 30., 37.5, 50.]

#%Kippschlupf
Skn=R2 / (sig * X2);
Sk(i) = Skn * f1n / fArr[i]

#%Schlupf
S(s,i) = s * Sk(i)

#%Spannungen
U1N=400;
array UdivArr[7] = [5.0, 2.0, 1.43, 1.0, 1.0, 1.0, 1.0]
U(i) = U1N / UdivArr[i]

#%Drehzahlen
n(s,i) = nsArr[i] * (1-S(s,i))

#%Kippmomente
Mkn= (3*(U1N**2)*(1-sig)) / (4 * pi * ns * sig * X1 );
Mk(i) = Mkn*(((U(i)/U1N)*(f1n/fArr[i]))**2);

#%Drehmomente
Mn=33.705;
Mi(s,i) = (Mk(i)*2.0 ) / ( ( S(s,i)/ Sk(i) ) + ( Sk(i)/S(s,i) ) )

array myColor[7] = ['blue', 'red', 'orange', 'dark-violet', 'green', 'light-blue', 'brown' ]
set title "M = f(n)"
set key reverse
set xlabel "n / 1/s"
set ylabel "M / Nm"
set grid xtics, ytics

plot for [i=1:7] [s=0:1.5:0.01] '+' u (2*n(s,i)):(Mi(s,i)) w l lw 2 lc rgb myColor[i] title sprintf("%g Hz",fArr[i])

### end of code

Addition: (some explanations for the plot command)

  • plot for [i=1:7] a loop of 7 plots into one graph with index i
  • [s=0:1.5:0.01] '+' this "generates" values for s as parameter from 0 to 1.5 in 0.01 steps
  • u (2*n(s,i)) using the x-value from function with s and i as parameter
  • :(Mi(s,i)) y-value from function with s and i as parameter
  • w l lw 2 with lines linewidth 2
  • lc rgb myColor[i] take the linecolor from the array myColor
  • title sprintf("%g Hz",fArr[i]) title use from array fArr

Check gnuplot help for the keywords, e.g. help plot for, help sample, help sprintf, etc.

Result:

enter image description here