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

조회 수: 2 (최근 30일)
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
SonOfAFather
SonOfAFather 2012년 8월 29일
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.
clear;
clc;
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)
figure(1)
plot(t,f);
xlabel('t (sec)');
ylabel('f(t)');
Error using *
Inner matrix dimensions must agree.
Error in tesst (line 8)
f = 22*cos(15*pi*t)*e.^(-0.5*t);

댓글을 달려면 로그인하십시오.

채택된 답변

Matt Tearle
Matt Tearle 2012년 8월 29일
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);

추가 답변 (1개)

Wayne King
Wayne King 2012년 8월 29일
편집: Wayne King 2012년 8월 29일
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);
plot(x,y)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by