MATLAB help with functions
Info
This question is closed. Reopen it to edit or answer.
Show older comments
>> 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
Answers (2)
John D'Errico
on 23 Oct 2019
Edited: John D'Errico
on 23 Oct 2019
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));
1 Comment
rayan sharqawi
on 23 Oct 2019
This question is closed.
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!