如何正确计算非线性函数并在 Octave 中绘制其图形? [英] How to correctly calculate a nonlinear function and plot its graph in Octave?

查看:35
本文介绍了如何正确计算非线性函数并在 Octave 中绘制其图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标: 使用非线性函数绘制图形.函数和图形

Goal: Plot the graph using a non-linear function. Function and graph

这是我第一次在 Octave 工作.要绘制图形,我需要计算 Fx (0.1 ... 10) 范围内的函数.

This is my first time working at Octave. To plot the graph, I need to calculate a function in the range Fx (0.1 ... 10).

我尝试通过 for 循环循环函数,将结果写入数组(x 轴 - Fn,y 轴 - 函数值),然后将数组加载到 plot() 函数中来实现这一点.

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.

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.

>> 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.

问题:

  1. 我是否选择了正确的方法来解决问题?有没有更优雅的方式?
  2. 我该如何解决这个错误?

谢谢!

推荐答案

如果我正确解释了您要执行的操作,则以下内容应该有效.首先,您需要使用作用于 Fn 的所有算术运算符的逐项版本.除了前面有一个点之外,它们与普通运算符相同.接下来,您需要将 Fn 等于一个向量,该向量包含要绘制的所有点的 x 值,并将 Q 等于一个向量,该向量包含要为其绘制曲线的 Q 值.使用 for 循环遍历 Q 的值并在循环的每次迭代中绘制一条曲线.您不需要循环来绘制每条曲线,因为 Octave 将应用您的测试"曲线.函数到整个 Fn 向量,并将结果作为相同大小的向量返回.要在对数轴上绘制曲线,请使用函数semilogx(x, y)";plot(x, y)"的插图.为了使图出现在同一个图上,而不是分开的图,将保持"在循环之前和推迟"然后.您在 for 循环中使用了元胞数组而不是向量,绘图函数不接受这种情况.此外,您不需要在 Octave 函数中使用显式 return 语句.

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. 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. Use a for-loop to loop through the values of Q and plot a single curve in each iteration of the loop. 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. To plot the curves on a log axis, use the 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. You used cell arrays instead of vectors in your for-loop, which the plotting functions don't accept. Also, you don't need an explicit return statement in an 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

这篇关于如何正确计算非线性函数并在 Octave 中绘制其图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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