MATLAB help with functions

>> x=[0:1:10]
x =
0 1 2 3 4 5 6 7 8 9 10
>> y=(exp(1).^(0.2*x/0.2))*cos((100-1).^(1/x))
Error using /
Matrix dimensions must agree.
>> y=(exp(1).^(0.2*x/0.2))*(cos((100-1).^(1/x))
y=(exp(1).^(0.2*x/0.2))*(cos((100-1).^(1/x))
↑
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Did you mean:
>> y=[exp(1).^(0.2*x/0.2)]*[cos((100-1).^(1/x))]
Error using /
Matrix dimensions must agree.
>>
Im trying to insert this function to plot a graph
i already have the values of k,m and c

回答(2 个)

John D'Errico
John D'Errico 2019-10-23
编辑:John D'Errico 2019-10-23
Ok, you know that you have to use .^ to raise to a power. Then look carefully at the error message. What does it tell you?
"Error using /
Matrix dimensions must agree."
MATLAB did not like the / operator there? Why not? Because you did not use ./ instead.
You use the dotted operators to tell MATLAB to operate element-wise on a vector or array.
Where do we see a / in there?
y=(exp(1).^(0.2*x/0.2))*(cos((100-1).^(1/x))
I see two spots.
x/0.2
and
1/x
The fitrst case is not a problem, because MATLAB is able to divide a matrix or vector by a constant scalar with no problem. It is the second case where you have an issue. You need to use 1./x instead.
Oh, by the way, this is a silly thing to do here:
exp(1).^(0.2*x/0.2)
You clearly know what exp does!!!! So you might write
exp(0.2*x/0.2)
But even there, then why not make things even simpler? The last time I checked, we learned that:
0.2*x/0.2 == x
So you could just write your expression as:
y = exp(x)*(cos((100-1).^(1./x));
rayan sharqawi
rayan sharqawi 2019-10-23

0 个投票

I still got this errorCapture.PNG

此问题已关闭。

产品

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by