Home > Software engineering >  Output doesn't come. Plotting issue [Matlab to python conversion]
Output doesn't come. Plotting issue [Matlab to python conversion]

Time:05-14

This is my Pyhton code.

import numpy as np
import matplotlib.pyplot as plt


n = 3;                          #% No of image
T = 100;                                            #% for the time period user wants to see the mirage

ts = .2*(100/(2*n-3));                              #% standing time
tv = .6*((100-((2*n-3)*ts))/(2*(n-1)));             #% travelling time

m1 =   np.arange(0,tv,0.1); 
x1 = 0.5*(1-(np.cos(np.pi*(m1/tv)))); 
xa = x1;



x2 = []
#travelling toward from left to right
for i in np.arange(1,n-1):
    x1 = x1 i -1
    
#standing at one point
    for f in np.arange(1,ts):
        x2.append(x1[-1])
    if i==1:
        x3 = [x1, x2]
    else:
        x3 = [x3,x1,x2] 
        
        
        

#%travelling from right to left

xd = np.flip(xa)
for i in range(0, n - 1):
    if i == 0:
        pass
    else:
        xd = xd - 1
    xw = []
    for f in np.arange(1, ts):
        mini = np.amin(xd)
        xw.append(mini)
    if i == 0:
        xm = np.concatenate((xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)
    else:
        xm = np.concatenate((xm, xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)

xm = abs(np.amin(xm))   xm


#%cloning the cycle

xs = []
for r in np.arange(1, np.fix(T/(2*(n-1)*(tv ts)))):
    if r==1:
        xs.append[x3,xm]
    else:
        xs.append[xs,x3,xm]
        
plt.plot(xs)

But when I run it this nothing comes out

enter image description here

And this is my matlab code

clc;
close all;
clear all;

% all of the given time are in milisecond
n = 3;% No of image
T = 100;% for the time period user wants to see the mirage

ts = .2*(100/(2*n-3)); % standing time
tv = .6*((100-((2*n-3)*ts))/(2*(n-1)));% travelling time

m1 = 0:.1:tv;
x1 = .5*(1-cos(pi*(m1/tv)));%position equation
xa = x1;

 
%travelling toward from left to right
for i= 1:n-1
    
x1 = x1 i-1;
   
%standing at one point
 for f = 1:ts
     x2(f) = x1(end);
 end
 
 if i==1
     x3=[x1,x2];
 else
     x3 = [x3,x1,x2];
 end
 
end
disp(x2)
 
%travelling from right to left

xd = flip(xa);

for i= 1:n-1
    if i==1
    else
        xd = xd-1;
    end
    
    %standing at one point
 for f = 1:ts
     xw(f) = min(xd);
 end

 if i==1
     xm=[xd,xw];
 else
     xm = [xm,xd,xw];
 end
 
end
xm = abs(min(xm)) xm;
 
#%cloning the cycle

xs = []
for r in np.arange(1, np.fix(T/(2*(n-1)*(tv ts)))):
    if r==1:
        xs.append(x3,xm)
    else:
        xs.append(xs,x3,xm)
        
plt.plot(xs)

enter image description here

And this is output that comes with matlab code. I want the output of my python code to same as Matlab code or at least I want to see a plot. But it doesn't show anything. So how can I solve this issue? Thank you in advance.

CodePudding user response:

Here is a fixed version of your code. The basic idea is not to mix concatenate with append. In MATLAB, [[a],[b]] is a horizontal concatenation and is different from list.append() in Python. Also take care of the inclusive range in MATLAB vs. exclusive range in Python, and fix returns a float whereas int returns an integer.

import numpy as np
import matplotlib.pyplot as plt

#  all of the given time are in milisecond
n  = 3                                  # No of image
T  = 100                                # for the time period user wants to see the mirage
ts = 0.2*(100/(2*n-3))                  # standing time
tv = 0.6*((100-((2*n-3)*ts))/(2*(n-1))) # travelling time
m1 = np.arange(0,tv .1,0.1)
x1 = 0.5*(1-np.cos(np.pi*(m1/tv)))      # position equation
xa = x1


# travelling toward from left to right
x2 = np.zeros(int(ts))
for i in range(1,n):
    x1 = x1 i-1
    # standing at one point
    for f in np.arange(0,int(ts)):
        x2[f] = x1[-1]
    if i == 1:
        x3 = np.concatenate((x1,x2))
    else:
        x3 = np.concatenate((x3,x1,x2))
    

# # travelling from right to left
xd = xa[::-1] # flip xa
xw = np.zeros(int(ts))
for i in range(1,n):
    if i != 1:
        xd -= 1
    # standing at one point
    for f in np.arange(0,int(ts)):
        xw[f] = np.min(xd)
    if i == 1:
        xm = np.concatenate((xd,xw))
    else:
        xm = np.concatenate((xm,xd,xw))

xm  = np.abs(np.min(xm))

# cloning the cycle
for r in range(0,int(T/(2*(n-1)*(tv ts)))):
    if r == 0:
        xs = np.concatenate( (x3,xm) ) 
    else:
        xs = np.concatenate( (xs,x3,xm) )

plt.plot(xs)

which shows the same plot as MATLAB.

enter image description here

  • Related