Clear Filters
Clear Filters

how to use the mathmatical constant "e" in conjunction with a vector.

9 views (last 30 days)
I am trying to use the mathmatical constant "e" in conjunction with a vector and each time i rum my script i tells me that the " * " is wrong. I have narrowed it down by removign things one piece at a time and everything runs great until i add the e back into my script. i saw somewhere that you can use exp() to make it work but in conjunction with the rest of the function i am not getiing it to work.
  1 Comment
SonOfAFather on 29 Aug 2012
i really don't like assigning e = exp(1) but if that works thats's fine. I would like to get this function done in a oneliner. here is what i have so far.
close all;
% create vector of time values
t = 0.0 : 0.01 : 5.0;
% evaluate function at time points
e = exp(1);
f = 22*cos(15*pi*t)*e.^(-0.5*t);
% plot t vs f(t)
xlabel('t (sec)');
Error using *
Inner matrix dimensions must agree.
Error in tesst (line 8)
f = 22*cos(15*pi*t)*e.^(-0.5*t);

Sign in to comment.

Accepted Answer

Matt Tearle
Matt Tearle on 29 Aug 2012
Your problem isn't the e, it's that the two parts you're trying to multiply are both vectors, hence you need .* instead of *:
f = 22*cos(15*pi*t) .* e.^(-0.5*t);
% ---vector--- ---vector--
But, as Wayne King says, don't use e.^x, use exp(x):
f = 22*cos(15*pi*t) .* exp(-0.5*t);

More Answers (1)

Wayne King
Wayne King on 29 Aug 2012
Edited: Wayne King on 29 Aug 2012
You need to post some code so we can see where you encounter an error. You can certainly use exp() with vectors.
x = 1:0.001:10;
y = exp(x);
% or
z = exp(1)*ones(100,1);
Of course, you can always define a variable.
e = exp(1);
but I don't think you should need to do that.
Also, don't forget the "dot" notation if that is necessary.
x = 1:0.001:10;
y = exp(-x.^2);

Community Treasure Hunt

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

Start Hunting!