简体   繁体   English

如何在 Octave 中正确计算非线性 function 和 plot 其图形?

[英]How to correctly calculate a nonlinear function and plot its graph in Octave?

Goal: Plot the graph using a non-linear function. Function and graph目标: Plot 使用非线性图 function. Function 和图

在此处输入图像描述

在此处输入图像描述

This is my first time working at Octave.这是我第一次在 Octave 工作。 To plot the graph, I need to calculate a function in the range Fx (0.1... 10).对于 plot 图形,我需要计算 Fx (0.1...10) 范围内的 function。

I tried to implement this by looping the function through the for loop, writing the results to an array (x-axis - Fn, y-axis - function value), then loading the arrays into the plot() function.我尝试通过 for 循环循环 function 来实现这一点,将结果写入数组(x 轴 - Fn,y 轴 - function 值),然后将 arrays 加载到 plot() function 中。

Fn = 1
Ln = 5
Q  = 0.5

function retval = test (Fn, Ln, Q)
  # Fn squared (for common used)
  Fn = Fn^2
  # Node A + Node B
  nodeA = Fn * (Ln - 1)
  nodeB = (Ln * Fn - 1)^2 + Fn * (Fn - 1)^2 * (Ln - 1)^2 * Q^2
  nodeB = sqrt(nodeB)
  # Result
  result = nodeA / nodeB
  retval = result
  return;
endfunction


frequencyArray = {}
gainArray = {}
fCount = 1
gCount = 1

for i = 0:0.5:5
  # F
  Fn = i
  frequencyArray{fCount} = Fn
  fCount = fCount + 1
  # G
  gainArray{gCount} = test(Fn, Ln, Q)
  gCount = gCount + 1
end

plot(frequencyArray, gainArray);

As a result, I get an error about the format of the arrays.结果,我收到有关 arrays 格式的错误。

>> plot(frequencyArray, gainArray);
error: invalid value for array property "xdata"
error: __go_line__: unable to create graphics handle
error: called from
    __plt__>__plt2vv__ at line 495 column 10
    __plt__>__plt2__ at line 242 column 14
    __plt__ at line 107 column 18
    plot at line 223 column 10

In addition to the error, I believe that these tasks are solved in more correct ways, but I did not quite understand what to look for.除了错误之外,我相信这些任务都以更正确的方式解决了,但我不太明白要寻找什么。

Questions:问题:

  1. Did I choose the right way to solve the problem?我是否选择了正确的方法来解决问题? Are there any more elegant ways?有没有更优雅的方法?
  2. How can I fix this error?我该如何解决这个错误?

Thank you!谢谢!

If I have correctly interpreted what you are trying to do, the following should work.如果我正确地解释了您要执行的操作,则以下内容应该有效。 Firstly, you need to use the term-by-term versions of all arithmetic operators that act on Fn.首先,您需要使用作用于 Fn 的所有算术运算符的逐项版本。 These are the same as the normal operators except preceded by a dot.这些与普通运算符相同,只是前面有一个点。 Next, you need to put Fn equal to a vector containing the x-values of all the points you wish to plot and put Q equal to a vector containing the values of Q for which you want to draw curves.接下来,您需要将 Fn 等于一个向量,其中包含您希望到 plot 的所有点的 x 值,并将 Q 等于一个向量,该向量包含您要为其绘制曲线的 Q 的值。 Use a for-loop to loop through the values of Q and plot a single curve in each iteration of the loop.使用 for 循环在循环的每次迭代中通过 Q 和 plot 的值循环一条曲线。 You don't need a loop to plot each curve because Octave will apply your "test" function to the whole Fn vector and return the result as a vector of the same size.您不需要循环到 plot 每条曲线,因为 Octave 会将您的“测试” function 应用于整个 Fn 向量,并将结果作为相同大小的向量返回。 To plot the curves on a log axis, use the function "semilogx(x, y)" insetad of "plot(x, y)".要 plot 对数轴上的曲线,请使用 function "semilogx(x, y)" insetad of "plot(x, y)"。 To make the plots appear on the same figure, rather than separate ones put "hold on" before the loop and "hold off" afterwards.为了使绘图出现在同一个图形上,而不是单独的图形,在循环之前放置“hold on”并在循环之后“hold off”。 You used cell arrays instead of vectors in your for-loop, which the plotting functions don't accept.您在 for 循环中使用单元格 arrays 而不是向量,绘图函数不接受。 Also, you don't need an explicit return statement in an Octave function.此外,您不需要在 Octave function 中显式返回语句。

The following code produces a set of curves that look like the ones in the figure you pasted in your question:以下代码生成一组曲线,看起来像您在问题中粘贴的图中的曲线:

Ln = 5

function result = test (Fn, Ln, Q)
    # Fn squared (for common used)
    Fn = Fn.^2;
    # Node A + Node B
    nodeA = Fn .* (Ln - 1);
    nodeB = (Ln .* Fn .- 1).^2 + Fn .* (Fn .- 1).^2 .* (Ln - 1)^2 * Q^2;
    nodeB = sqrt(nodeB);
    # Result
    result = nodeA ./ nodeB;
endfunction

Fn = linspace(0.1, 10, 500);
Q = [0.1 0.2 0.5 0.8 1 2 5 8 10];

hold on
for q = Q
    K = test(Fn, Ln, q);
    semilogx(Fn, K);
endfor
hold off

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

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