How to put trigonometric numbers as a power in matlab - Stack Overflow
Raising a matrix to a power
raise power to power in title
How to raise a decimal numbers as power of A??
Videos
Let's cover all the points in no particular order:
The
eis known as Euler's constant, where e ~ 2.71828...
Something like ex, i.e. raisingeto the power ofxis known as the exponential function. While you could in theory computeeand then use thepoweroperator (^in matlab) to raiseeto that power accordingly, this is in fact a less precise way of calculating the exponential function, and therefore matlab provides theexpfunction for that purpose. If you pass an array [x1, x2,...] toexp, it will perform this function "elementwise", and return [ex1, ex2,...] appropriately.The
sinfunction, similarly, if given an array of numbers, will calculate thesinfor each of those numbers, and return an array of the same shape as its input.You could use
fplotwith an anonymous function as Neo suggested, but I find that beginners find that confusing. So instead I would suggest you create an array of values between [-2,2], and obtain the value of y for each of them, which in matlab can be done as a single operation because it's good at working with arrays and performing such 'vectorized' operations:Copyx = [-2:0.1:2]; % Create an array of values from -2 to 2, with a step of 0.1 % Note: the ';' at the end suppresses output; if you want to % see the contents of your array, remove it at the end.Now that you have your array
xyou can perform operations on it. By convention, matlab uses "dot-operators" to denote "elementwise" operations, as opposed to 'undotted' ones denoting primarily "matrix" operations. Therefore to raise all elements of the arrayxto the power of 6, you would dox .^ 6
With that in mind, you can now calculate your f(x) for each point in the arrayx, by using elementwise operations:Copyy = exp( sin(x).^3 ) + x.^6 - 2*(x.^4) - x.^3 - 1;The result is an array
yof the same size asx.You can now plot this using the
plotcommand, which takes two arrays of the same size and plots all points in the first array against their equivalent points in the second array, as (x,y) pairs:Copyplot( x, y );
Output:

As you can see, matlab 'connects' the points by default. If you wanted to see just the individual points of your array instead, you can specify this as the third argument:
Copyplot(x, y, 'o');

Type help plot at the matlab terminal to see more options for the plot command.
PS: The above plots were made in octave rather than in matlab, because I don't have matlab at home. Your own plots may look slightly different.
For a simple one-liner you could use:
Copyfplot(@(x) exp(sin(x).^3) + x.^6 - 2*x.^4 - x.^3 - 1, [-2 2]);
See fplot.
So, the attached image is what I need to plot. I've written out some code as well, but I'm having some trouble raising e to the exponent.
Any help to my code or how to approach the problem is much appreciated.
The code:
t=0:0.1:1;
a=3;
f = 5;
w = 2*pi*f;
phi = -pi/4;
y1=a*exp(t*(-3))*cos((w.*t)+phi);
plot(t,y1)
The error message I get looks like this: