Plotting f(x) = cosx sin(2x) and its derivative for x in [-pi, pi]

In summary, the conversation discusses how to plot the function f(x) = cos(x)sin(2x) and its derivative on the same plot for the range of -π ≤ x ≤ π. The person is having trouble with an error and asks for suggestions. They eventually figure out the issue and provide a solution that includes using the operator "." for multiplication and adding a legend and labeling the axes.
  • #1
18
0
QUESTION:

Plot the function f(x) = cosx sin(2x) and its derivative, both on the same
plot, for π ≤ x ≤ π . Plot the function with a solid line, and the derivative with
a dashed line. Add a legend and label the axes.


Okay, so this is what I have so far in a script file...


x = [-pi: 0.01: pi];

y = cos(x)*sin(2*x);

yd = 2*cos(x)*cos(2*x)-sin(x)*sin(2*x);

plot (x,y,x,yd)


But then I get this error:

? Error using ==> mtimes
Inner matrix dimensions must agree.

Error in ==> p11 at 3
y = cos(x)*sin(2*x);


I'm not worried about the details, that should be easy enough. (dotted/solid lines and legends and axes labeling.) I just want to make sure I can get it to plot. Also it asks to plot for π ≤ x ≤ π, but i have a feeling it means -π ≤ x ≤ π.

It looks pretty simple, but I'm still learning this program and it's proving to be more difficult than it should be. Anyone have any ideas what's wrong with my script?

Thanks!
 
Physics news on Phys.org
  • #2
Try taking the brackets off of x

x = -pi : 0.01 : pi;
 
  • #3
LabGuy330 said:
Try taking the brackets off of x

x = -pi : 0.01 : pi;

That didn't work, but searched the error and I found out that there is an operator "." that you're supposed to use for multiplication.


x = [-pi: 0.01: pi];

y = (cos(x)).*(sin(2.*x));

yd = 2.*cos(x).*cos(2.*x)-sin(x).*sin(2.*x);

plot (x,y)

hold on

plot (x,yd, '--k')

legend('y = (cos(x)).*(sin(2.*x))', 'yd = 2.*cos(x).*cos(2.*x)-sin(x).*sin(2.*x)')

xlabel ('x-axis')

ylabel ('y-axis')

hold off


thank you though
 

What is MATLAB function plotting?

MATLAB function plotting is a way to visualize and analyze data using MATLAB software. It allows you to create graphs and charts from mathematical functions and data sets.

How do I plot a function in MATLAB?

To plot a function in MATLAB, you can use the plot command followed by the function. For example, plot(x, y) will plot the function y against the variable x.

What types of plots can be created in MATLAB?

MATLAB offers a variety of plot types, including line plots, scatter plots, bar graphs, histograms, and more. You can also customize the appearance of these plots with different colors, labels, and styles.

Can I add multiple functions to one plot in MATLAB?

Yes, you can plot multiple functions on the same graph by using the hold on command before each plot command. This will allow you to compare and analyze multiple functions at once.

How can I save a MATLAB plot as an image?

To save a MATLAB plot as an image, you can use the saveas function and specify the file format (such as .png or .jpg). Alternatively, you can use the print function to save the plot as an image or a PDF file.

Suggested for: Plotting f(x) = cosx sin(2x) and its derivative for x in [-pi, pi]

Replies
3
Views
709
Replies
6
Views
977
Replies
3
Views
792
Replies
6
Views
753
Replies
1
Views
875
Replies
5
Views
1K
Replies
10
Views
1K
Replies
4
Views
927
Back
Top