简体   繁体   English

如何仅绘制所需的值?

[英]How do I only plot the values I want?

I currently have the code and I having some trouble trying to plot it, I know that trying to plot both ymax and y won't work in this case, but how would I go about plotting just the value for y? 我目前有代码,尝试绘制它时遇到了一些麻烦,我知道尝试同时绘制ymax和y都行不通,但是我将如何仅绘制y的值呢? I have plotted the function before by removing the ymax from the return, but I need to print the values and plot the solution for y. 我通过从返回中删除ymax来绘制函数,但是我需要打印值并绘制y的解。

import numpy as np
import matplotlib.pyplot as plt

def GaussElimination(A):
    '''
    Description: Use Gauss elimination to solve a set of simultaneous equations
    Parameters: A  a matrix of coefficient and constant value for the system
    Return: a matrix holding the solution to the equation.  This corresponds to the last n 
    '''
    nr,nc=A.shape
    B= A.copy()

    # start the gauss elimination
    for r in range(nr):
        #pivoting
        max=abs(B[r][r])
        maxr = r
        for rr in range(r,nr):
            if max < abs(B[rr][r]):
                max = abs(B[rr][r])
                maxr = rr

        if max == 0:
            print("Singular Matrix")
            return []

        # swap if needed
        if (maxr != r):
            for c in range(nc):
                temp = B[r][c]
                B[r][c]=B[maxr][c]
                B[maxr][c] = temp

        # scale the row
        scale = B[r][r]
        for c in range(r,nc):
            B[r][c] = B[r][c]/scale

        # eliminate values in the columns
        for rr in range(nr):
            if rr != r:
                scale = B[rr][r]
                for c in range(r,nc):
                    B[rr][c]=B[rr][c] - scale*B[r][c]

    if (nc == nr+1):
        return B[:,nc-1]
    else:
        return B[:,(nr):nc]

def SimplySupportedBeam(n):
    M = np.zeros([n+1,n+1])
    C = np.array([[0],[150],[0],[0],[0],[0]])
    for r in range(n-3):
        M[r][r] = 1
        M[r][r+1] = -4
        M[r][r+2] = 6
        M[r][r+3] = -4
        M[r][r+4] = 1

    M[n-3][1] = 1
    M[n-2][n-1] = 1

    M[n-1][n-5] = 1
    M[n-1][n-4] = -2
    M[n-1][n-3] = 1
    M[n][n-2] = 1
    M[n][n-1] = -2
    M[n][n] = 1

    A = np.concatenate((M,C), axis=1)
    y0 = GaussElimination(A)
    y = y0[1:n]
    ymax = np.amax(abs(y))

    return y, ymax



n = int(input("Index of the last node: "))
print (SimplySupportedBeam(n))

plt.figure(1)
plt.plot(SimplySupportedBeam(n))
plt.show()

How would I plot just the value I get for y from my code? 我如何只绘制从代码中得到的y值?

It seems like y is 1D numpy array. 似乎y是一维numpy数组。

If you just want to plot its values against their indices you should be able to do so using either 如果您只想根据其索引绘制其值,则可以使用

plt.plot(SimplySupportedBeam(n)[0])

or 要么

y, ymax = SimplySupportedBeam(n)
plt.plot(y)

The problem was that your function returns two values, ie y and ymax . 问题在于您的函数返回两个值,即yymax (I did not (我没有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM