Create an Interactive Animation of Sinusoidal Functions Using MatLab

In summary, the code creates an animation of a function with varying parameters using the meshgrid function and a while loop. However, the code is not working correctly as the x and y values are not being updated in the loop, resulting in a static graph. To fix this, the x and y matrices should be updated within the loop.
  • #1
MacLaddy
Gold Member
291
11

Homework Statement



Create an animation of the following:

Let x vary from -2pi to 2pi
Let y = sin(x)
Let z = sin(x-theta) * cos(y-theta)

Let theta be the animation variable

remember, you will need to mesh x and y to create a two-dimensional
matrices: use the resulting arrays to find z

Homework Equations



The Attempt at a Solution



This is what I've tried so far, and it is following closely with an example in my book.

clear, clc, clf
X = [-2*pi:pi/50:2*pi];
theta = 0;
Y = sin(X);
[x,y] = meshgrid(X,Y);
z = (sin(x-theta) * cos(y-theta));
h = surf(z);
axis tight
set(gca,'nextplot','replacechildren');
%shading interp
%colormap(jet)

while theta < 4*pi;
z = sin(x-theta) * cos(y-theta);
theta = theta+(pi/50);
set(h,'ZData',z);
drawnow
end

I've been wracking my brain for a while over this, and I'm failing to see what isn't varying correctly. Any help, or a nudge in the right direction, will be appreciated.

Thanks,
Mac
 
Last edited:
Physics news on Phys.org
  • #2
Hi Mac,

Your code looks pretty good! The only thing I noticed is that in your while loop, you are not updating the x and y matrices. This means that the same x and y values are being used for each iteration, resulting in a static graph. To fix this, you can add the following lines inside your while loop:

[x,y] = meshgrid(X,Y);
z = sin(x-theta) * cos(y-theta);

This will update the values of x and y for each iteration, and your animation should work correctly.

Hope this helps!
 

Related to Create an Interactive Animation of Sinusoidal Functions Using MatLab

What is MatLab Animation Problem?

MatLab Animation Problem refers to a common issue faced by users when trying to create animations using the MatLab software. It is a problem that arises when there are errors in the coding or when the user is not familiar with the correct syntax for animation.

How do I fix MatLab Animation Problem?

To fix MatLab Animation Problem, you can start by checking your code for any errors or incorrect syntax. You can also refer to the MatLab documentation or online forums for help and solutions. Additionally, you can try breaking down your animation into smaller parts and testing them individually to identify the source of the problem.

Why is my MatLab animation not working?

There are several reasons why your MatLab animation may not be working. It could be due to incorrect coding, missing or outdated libraries, or hardware limitations. It is also possible that your animation is taking too long to render, resulting in the appearance of not working. It is important to troubleshoot and identify the specific issue in order to fix it.

Can I create custom animations in MatLab?

Yes, MatLab allows for the creation of custom animations using its built-in animation functions and tools. You can also incorporate your own code and algorithms to create unique and dynamic animations in MatLab.

Is there a limit to the complexity of animations I can create in MatLab?

There is no specific limit to the complexity of animations that can be created in MatLab. However, it is important to keep in mind the hardware limitations and processing power of your computer. Complex animations may require more resources and may take longer to render, so it is important to optimize your code and use efficient algorithms.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
844
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
584
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
3
Views
574
  • Calculus and Beyond Homework Help
Replies
8
Views
889
  • Introductory Physics Homework Help
Replies
1
Views
210
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Special and General Relativity
Replies
7
Views
443
  • Advanced Physics Homework Help
Replies
19
Views
863
  • Calculus and Beyond Homework Help
Replies
4
Views
969
Back
Top