matlab plot遇到的小问题

在尝试用MATLAB绘制函数y=1/(x^2-1)时,遇到了错误,原因是矩阵维度不匹配。解决方法是在除法操作符前加上点号,如1./ (x.^2-1)。这确保了点对点操作。同样,通过设置x1=x2,可以使用plot函数绘制垂直线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Want to plot this function: y=1/(x^2-1)

 

>> x = -3:0.1:3;y = 1/(x.^2-1);

??? Error using ==> mrdivide

Matrix dimensions must agree.

 

>> x = -3:0.1:3;y = 1/(x^2-1);

??? Error using ==> mpower

Matrix must be square.

 

>> x = -3:0.1:3;y = (x^2-1);

??? Error using ==> mpower

Matrix must be square.

 

>> x = -3:0.1:3;y = (x.^2-1);

works...

but, how to plot y=1/(x^2-1)?

 

 

OK, here is the right answer, we need to add dot before divide operator, too

x = -3:0.1:3;y = 1./(x.^2-1);

 

analysis:

x  = -3:0.1:3; -> makes x a array of size 1x31

m = (x.^2-1); -> makes m a array of size 1x31,too

so, we do need add dot before divide operator ./, then y will be a array of size 1x31

 

 

some comment on http://www.experts-exchange.com/Programming/Languages/MatLab/Q_23558168.html

Error using ==> mpower . Matrix must be square

When 'x' is a vector or a matrix, just writing something like x^2, makes matlab think you want to square a matrix, which is only possible with square matrices.  If you have a row vector, then you have a 1 x N matrix, so unless N = 1, you don't have a square matrix, and you get an error.  If you merely want to square each entry in x, then you use the " .^ " operator to indicate point-by-point operations (such as division, multiplication, or raising to a power).  Whether you need the "dot" before the symbol (/ * or ^) depends on what you're doing.  From the looks of it to me, you need the dot if you have a row vector that you only want to use to simulate continuous time.

 

2010-11-16  11:01:40

 

------------------------------------------------------------------------------------------------------------------分界线

如何用plot画竖直线

in matlab how to use plot to draw a vertical line

plot([x1,x2],[y1,y2]);

this can draw a line from (x1,y1) to (x2,y2)

if you want to draw a vertical line

then make x1=x2;

the horizontal  line is the same;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值