简体   繁体   English

MATLab,用函数绘制图形

[英]MATLab, plotting a graph with a function

I'm having trouble writing this code.我在编写此代码时遇到问题。 So, I'm trying to make a code for所以,我正在尝试为

func = y*x(n) + z * x(n)函数 = y*x(n) + z * x(n)

All the values are arbitrary and x(n) is the value at the position n.所有值都是任意的,x(n) 是位置 n 处的值。 I need to plot a graph at each nth position.我需要在每个第 n 个位置绘制一个图形。 So if x(1) = 5 I plot a point at when x=1 and y=5.因此,如果 x(1) = 5,我会在 x=1 和 y=5 时绘制一个点。 The issue is that I can't figure out how to make an arbitrary array and don't know how to get the answer for func when I add x(n) value at the nth position.问题是当我在第 n 个位置添加 x(n) 值时,我无法弄清楚如何制作任意数组,也不知道如何获得 func 的答案。 I also am having trouble plotting a graph, but think this is because I can't figure out to use the array yet.我也无法绘制图形,但我认为这是因为我无法弄清楚如何使用该数组。

I'm new to MatLab.我是 MatLab 的新手。

So if I am following y & z are just constants?因此,如果我遵循 y & z 只是常量? I think the confusion is typically this would be written something like "y = a x +b x"我认为混淆通常是这样写的“y = a x + b x”

Like Cris Luengo mentioned in the comments above, you should should go over some basic Matlab tutorials as this is very basic.就像上面评论中提到的 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)

This should do the trick:这应该可以解决问题:

% 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));

Refer to this page for more information about anonymous functions.有关匿名函数的更多信息,请参阅此页面

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

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