[英]MATLab, plotting a graph with a function
我在编写此代码时遇到问题。 所以,我正在尝试为
函数 = y*x(n) + z * x(n)
所有值都是任意的,x(n) 是位置 n 处的值。 我需要在每个第 n 个位置绘制一个图形。 因此,如果 x(1) = 5,我会在 x=1 和 y=5 时绘制一个点。 问题是当我在第 n 个位置添加 x(n) 值时,我无法弄清楚如何制作任意数组,也不知道如何获得 func 的答案。 我也无法绘制图形,但我认为这是因为我无法弄清楚如何使用该数组。
我是 MatLab 的新手。
因此,如果我遵循 y & z 只是常量? 我认为混淆通常是这样写的“y = a x + b x”
就像上面评论中提到的 Cris Luengo 一样,您应该阅读一些基本的 Matlab 教程,因为这是非常基本的。
% y and Z are constants
y = 1;
z = 2;
%this makes x = [0,1,2,...10];
x = 0:1:10;
func = y.*x + z.*x;
plot(func)
这应该可以解决问题:
% Define X as a range between -10 to 10 (+1 on every step)...
x = -10:10;
% Define your constants...
y = 3;
z = -1;
% Define the function...
fun = @(x) (y .* x) + (z .* x);
% Plot X on the x-axis and fun(x) on the y-axis...
% fun(x) numerically evaluates fun for the given x
plot(x,fun(x));
有关匿名函数的更多信息,请参阅此页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.