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

Click For Summary
SUMMARY

The discussion focuses on plotting the function f(x) = cos(x) * sin(2x) and its derivative over the interval [-π, π] using MATLAB. The user initially encounters an error due to incorrect matrix multiplication syntax. The solution involves using the element-wise multiplication operator (.) to correctly compute the function and its derivative. The final script successfully plots both the function and its derivative with appropriate labels and a legend.

PREREQUISITES
  • Basic understanding of MATLAB syntax and plotting functions
  • Knowledge of element-wise operations in MATLAB
  • Familiarity with trigonometric functions and their derivatives
  • Understanding of plotting conventions, including legends and axis labeling
NEXT STEPS
  • Learn MATLAB element-wise operations and their importance in vectorized calculations
  • Explore MATLAB's plotting functions, including customization options for legends and line styles
  • Study trigonometric identities and their applications in calculus
  • Investigate MATLAB error handling and debugging techniques for common coding issues
USEFUL FOR

Students learning MATLAB, educators teaching calculus and programming, and anyone interested in visualizing mathematical functions and their derivatives.

OUmecheng
Messages
18
Reaction score
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
Try taking the brackets off of x

x = -pi : 0.01 : pi;
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
875
  • · Replies 1 ·
Replies
1
Views
898
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K