python-> scilab翻译错误

时间:2018-06-27 14:17:59

标签: python graph translation scilab

我的scilab没有显示任何有关尝试从python到scilab的“ Oscillateursmécaniquescouplés”问题的信息,但它应该绘制3个表示振荡的图形

这是python:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
m1=1;m2=1;k1=1;k2=1;k3=1;m=1;k=1;a=1;
def equadiff(var,t):
    x1=var[0]
    x2=var[1]
    v1=var[2]
    v2=var[3]
    a1=( k2*x2-(k1+k2)*x1)/m1
    a2=( k2*x1-(k2+k3)*x2)/m2
    return ([v1,v2,a1,a2])
def Oscillateur(x10,x20,v10,v20,tmax):
    t=np.linspace(0,tmax,300)
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1,aspect="equal")
    ax.spines["left"].set_position("zero")
    ax.spines["right"].set_color("none")
    ax.spines["bottom"].set_position("zero")
    ax.spines["top"].set_color("none")
    ax.xaxis.set_ticks_position("bottom")
    ax.yaxis.set_ticks_position("left")
    plt.axis("tight")
    var0=[x10,x20,v10,v20]
    solution=odeint(equadiff,var0,t)
    x1=solution[:,0]
    x2=solution[:,1]
    v1=solution[:,2]
    v2=solution[:,3]
    plt.plot(t,x1,t,x2)
    plt.axis("tight")
    plt.show()
Oscillateur(a,a,0,0,10)
Oscillateur(a,-a,0,0,10)
Oscillateur(2*a,0,0,0,50)

这是我的scilab,问题位于此代码内,我的猜测是那些ax.yaxis.set_ticks_position(“ left”),但我不知道如何解决它并显示我的图形:

m1=1;m2=1;k1=1;k2=1;k3=1;m=1;k=1;a=1;

function S=equadiff(var,t)
    x1=var(0)
    x2=var(1)
    v1=var(2)
    v2=var(3)
    a1=(k2*x2-(k1+k2)*x1)/m1
    a2=(k2*x1-(k2+k3)*x2)/m2
    disp([v1,v2,a1,a2])
endfunction

function T=Oscillateur(x10,x20,v10,v20,tmax)
  t=linspace(0,tmax,300)
  figure()
  ax=subplot(1,1,1,aspect="equal")
  var0=[x10,x20,v10,v20]
  solution=ode(var0,t0,t,equadiff)
  x1=solution(:,0)
  x2=solution(:,1)
  v1=solution(:,2)
  v2=solution(:,3)
  plot(t,x1,t,x2)
  a=gca()
  a.x_location = "zero";
  axis("tight");
endfunction 

disp(Oscillateur(a,a,0,0,10)) 
disp(Oscillateur(a,-a,0,0,10))
disp(Oscillateur(2*a,0,0,0,50))

1 个答案:

答案 0 :(得分:1)

这里有很多错误:

  1. equadiff返回未声明的S
  2. Scilab索引从1开始
  3. subplot没有名为“ aspect”的参数。
  4. t0未定义。
  5. equadiff参数必须按此顺序为t,var
  6. 如果t是行向量,则
  7. x自变量必须是列向量。
  8. Oscillateur返回未声明的T
  9. disp无法返回此空返回。
  10. 轴手柄的
  11. x_location属性不存在

修复所有这些错误会给您:

m1=1;m2=1;
k1=1;k2=1;k3=1;
m=1;
k=1;
a=1;

// added k1,k2,k3 m1,m2 as argument
function S=equadiff(t,x,k1,k2,k3,m1,m2)
    x1=x(1)
    x2=x(2)
    v1=x(3)
    v2=x(4)
    a1=(k2*x2-(k1+k2)*x1)/m1
    a2=(k2*x1-(k2+k3)*x2)/m2
    S=[v1;v2;a1;a2] // now equadiff returns an 4 by 1 vector
endfunction

// added k1,k2,k3 m1,m2 as argument
// Removed T as not used
function Oscillateur(x10,x20,v10,v20,tmax,equadiff,k1,k2,k3,m1,m2)
  t=linspace(0,tmax,300)
  figure()
  var0=[x10;x20;v10;v20] // x is 4 by 1 vector
  solution=ode(var0,0,t,list(equadiff,k1,k2,k3,m1,m2)) // k1 ,etc ar now passed as additionnal argument for equadiff
  // since equadiff returns a 4 by 1 vector and t is 1 by 300
  // solution is 4 by 300
  x1=solution(1,:)
  x2=solution(2,:)
  v1=solution(3,:)
  v2=solution(4,:)
  plot(t,x1,t,x2)
  legend('x1','x2')
  a=gca()
  //a.x_location = "zero"; //  what was that for ?
  set(a,"tight_limits","on");
endfunction 

// Display is only for printing value.
Oscillateur(a,a,0,0,10,equadiff,k1,k2,k3,k1,m2)
Oscillateur(a,-a,0,0,10,equadiff,k1,k2,k3,k1,m2)
Oscillateur(2*a,0,0,0,50,equadiff,k1,k2,k3,k1,m2)