如何创建一个具有2维的单个矢量? [英] How to create a Single Vector having 2 Dimensions?

查看:54
本文介绍了如何创建一个具有2维的单个矢量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将运动方程式(牛顿定律)用于简单的弹簧和质量场景,将其结合到给定的第二ODE方程y"中. +(k/m)x = 0; y(0)= 3; y'(0)= 0.

然后我能够运行一个代码,该代码可以计算精确解决方案并将其与Runge-Kutta方法解决方案进行比较.

它工作正常...但是,最近,我被要求不要将我的'x'和'v'值分开,而是使用具有两个维度(即'x'和'v '可以由x(1)和x(2)处理).

我的代码:

# Given is y" + (k/m)x = 0; y(0) = 3; y'(0) = 0

# Parameters
h = 0.01;  #Step Size
t = 100.0;  #Time(sec)
k = 1;
m = 1;
x0 = 3;
v0 = 0;

# Exact Analytical Solution
te = np.arange(0, t ,h);
N = len(te);
w = (k / m) ** 0.5;
x_exact = x0 * np.cos(w * te);
v_exact = -x0 * w * np.sin(w * te);

# Runge-kutta Method
x = np.empty(N);
v = np.empty(N);
x[0] = x0;
v[0] = v0;

def f1 (t, x, v):
    x = v
    return x
def f2 (t, x, v):
    v = -(k / m) * x
    return v

for i in range(N - 1):    #MAIN LOOP
    K1x = f1(te[i], x[i], v[i])
    K1v = f2(te[i], x[i], v[i])

    K2x = f1(te[i] + h / 2, x[i] + h * K1x / 2, v[i] + h * K1v / 2)
    K2v = f2(te[i] + h / 2, x[i] + h * K1x / 2, v[i] + h * K1v / 2)

    K3x = f1(te[i] + h / 2, x[i] + h * K2x / 2, v[i] + h * K2v / 2)
    K3v = f2(te[i] + h / 2, x[i] + h * K2x / 2, v[i] + h * K2v / 2)

    K4x = f1(te[i] + h, x[i] + h * K3x, v[i] + h * K3v)
    K4v = f2(te[i] + h, x[i] + h * K3x, v[i] + h * K3v)

    x[i + 1] = x[i] + h / 6 * (K1x + 2 * K2x + 2 * K3x + K4x)
    v[i + 1] = v[i] + h / 6 * (K1v + 2 * K2v + 2 * K3v + K4v)

任何人都可以帮助我了解如何创建具有2个维度的单个矢量,以及如何修改我的代码吗?

解决方案

您可以使用np.array()函数,这是您尝试执行的操作的示例:

x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

I have used the Equation of Motion (Newtons Law) for a simple spring and mass scenario incorporating it into the given 2nd ODE equation y" + (k/m)x = 0; y(0) = 3; y'(0) = 0.

I have then been able to run a code that calculates and compares the Exact Solution with the Runge-Kutta Method Solution.

It works fine...however, I have recently been asked not to separate my values of 'x' and 'v', but use a single vector 'x' that has two dimensions ( i.e. 'x' and 'v' can be handled by x(1) and x(2) ).

MY CODE:

# Given is y" + (k/m)x = 0; y(0) = 3; y'(0) = 0

# Parameters
h = 0.01;  #Step Size
t = 100.0;  #Time(sec)
k = 1;
m = 1;
x0 = 3;
v0 = 0;

# Exact Analytical Solution
te = np.arange(0, t ,h);
N = len(te);
w = (k / m) ** 0.5;
x_exact = x0 * np.cos(w * te);
v_exact = -x0 * w * np.sin(w * te);

# Runge-kutta Method
x = np.empty(N);
v = np.empty(N);
x[0] = x0;
v[0] = v0;

def f1 (t, x, v):
    x = v
    return x
def f2 (t, x, v):
    v = -(k / m) * x
    return v

for i in range(N - 1):    #MAIN LOOP
    K1x = f1(te[i], x[i], v[i])
    K1v = f2(te[i], x[i], v[i])

    K2x = f1(te[i] + h / 2, x[i] + h * K1x / 2, v[i] + h * K1v / 2)
    K2v = f2(te[i] + h / 2, x[i] + h * K1x / 2, v[i] + h * K1v / 2)

    K3x = f1(te[i] + h / 2, x[i] + h * K2x / 2, v[i] + h * K2v / 2)
    K3v = f2(te[i] + h / 2, x[i] + h * K2x / 2, v[i] + h * K2v / 2)

    K4x = f1(te[i] + h, x[i] + h * K3x, v[i] + h * K3v)
    K4v = f2(te[i] + h, x[i] + h * K3x, v[i] + h * K3v)

    x[i + 1] = x[i] + h / 6 * (K1x + 2 * K2x + 2 * K3x + K4x)
    v[i + 1] = v[i] + h / 6 * (K1v + 2 * K2v + 2 * K3v + K4v)

Can anyone help me understand how I can create this single vector having 2 dimensions, and how to fix my code up please?

解决方案

You can use np.array() function, here is an example of what you're trying to do:

x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

这篇关于如何创建一个具有2维的单个矢量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆