简体   繁体   English

如何用Octave绘制3个变量的函数?

[英]How to plot function of 3 variables with Octave?

I am new to Octave (and matlab for that matter). 我是Octave的新手(和Matlab有关)。 I have a function that looks like this 我有一个看起来像这样的功能

在此处输入图片说明

I would like to plot g(x,0.5,5) say. 我想画出g(x,0.5,5)说。

Here it is what I tried in Octave 这是我在八度尝试的

I defined an anonymous function 我定义了一个匿名函数

f=@(n,x,t) 1./n.*log(n.*pi.*t).*sin(n.*pi.*x);

then another anonymous function 然后是另一个匿名函数

g=@(m,x,t)x.^2+sum(f([1:m],x,t));

Finally defined 最后定义

x=-1:0.1:1;
plot(x,g(5,x,0.5))

but I get an error. 但我得到一个错误。 Is this the right way of plotting this function? 这是绘制此函数的正确方法吗? I must be doing a simple beginner error? 我一定是在做一个简单的初学者错误?

When you call f(n,x,t) , you are passing a 1-by-5 vector for n and a 1-by-21 vector for x . 调用f(n,x,t) ,您将传递n的1 x 5向量和x的1 x 21向量。 These have different numbers of elements, and therefore can't be multiplied element-by-element. 这些元素具有不同数量的元素,因此不能逐个元素相乘。 However, you can rewrite f to accommodate vectors for each and perform the sum from g by using matrix multiplication: 但是,您可以重写f以容纳每个向量,并通过使用矩阵乘法执行g的求和:

f = @(n, x, t) (1./n.*log(n.*pi.*t))*sin(pi.*n(:)*x);
g = @(m, x, t) x.^2 + f(1:m, x, t);

And now your plot will work: 现在您的情节将起作用:

x = -1:0.1:1;
plot(x, g(5, x, 0.5));

在此处输入图片说明

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

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