Info

此问题已关闭。 请重新打开它进行编辑或回答。

Matrix dimensions must agree

1 次查看(过去 30 天)
Dylan Girodat
Dylan Girodat 2020-2-25
关闭: MATLAB Answer Bot 2021-8-20
I am trying to plot a function in matlab and I am encountering the error: Matrix dimensions must agree.
my code is:
x=(1:14)
A=0.509998
mu1=0.387437
sigma1=0.0658118
mu2=0.384934
sigma2=0.0654886
la=5.82622e-10
y=A((1+(la/x.^12))*(1-exp(-(x-mu1)^2/(2*sigma1^2)))*(1-exp(-(x-mu2)^2/(2*sigma2^2))))
what is wrong with the function? Do I need . in other places?
Thanks for any help in advance.
Dylan

回答(2 个)

James Tursa
James Tursa 2020-2-25
编辑:James Tursa 2020-2-25
Yes, you need . in other places. Basically, to do element-wise operations you need to look for all of the x's in your equation and make sure the surrounding operations are element-wise. E.g., this
la/x.^2
should be
la./x.^2
And this
... )*( ...
should be
... ).*( ...
And this
(x-mu1)^2
should be
(x-mu1).^2
etc.
Also you have a typo. This
A(...
should be
A*(...
You might also double check to see if you want x.^12 or x.^2 in the equations.

John D'Errico
John D'Errico 2020-2-25
You understood that you needed to use a dotted operator here: x.^12
There are also ./ and .* operators. For a VERY good reason. USE THEM.
Of course, expect some possible numerical problems, because some of what you are trying to do will result in numerical garbage.
Look carefully at what values x takes on. Then look carefully at what you are doing with it.

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by