Animating sin(x-a): Tracking a Changing Title

  • Thread starter GreenPrint
  • Start date
  • Tags
    Tracking
In summary: Your code did not work because the title command was receiving a number instead of a string as its argument. In summary, to change the value of a in the title each time it goes through the loop, use the sprintf() function to format the string and then pass it as an argument to the title command.
  • #1
GreenPrint
1,196
0
THIS WAS RESOLVED

Homework Statement


In a problem I had to create a animation of the function
sin(x-a)
were a ranged from 0 to 8pi
Code:
x=[-2*pi:.00001*pi:2*pi];
a=0;
y=sin(x-a);
g=plot(x,y);
grid on;
title('sin(x-0)')
set(g,'EraseMode','xor')
while a<=8*pi
    a=a+.1*pi;
    y=sin(x-a);
    set(g,'ydata',y)
    drawnow
end
My code worked fine and did what it was suppose to do. I however was trying to get MATLAB to change the value of a in the title each time it went through the loop and I seem to be having difficulties doing this.

Homework Equations


The Attempt at a Solution


Here's my attempt at what I thought would produce what I was looking for.
Code:
x=[-2*pi:.00001*pi:2*pi];
a=0;
y=sin(x-a);
g=plot(x,y);
grid on;
title('sin(x-a)')
set(g,'EraseMode','xor')
while a<=8*pi
    a=a+.1*pi;
    y=sin(x-a);
    set(g,'ydata',y)
    drawnow
    title(fprintf('sin(x-%s)\n',num2str(a)));
end
I thought this would work. Before it enters the loop the title would get defined as sin(x-0). I thought that it would then redefine the title in this line
title(fprintf('sin(x-%f)\n',num2str(a)));
were num2str(a) would change a, which is defined as a number, into a string of text that could be placed in the title
sin(x-a)
That was my logic behind that... I also suppressed the output with the ";" operator

I thought that this would produce the results I was looking for but the title just gets changed to some kind of number like 14 or 13 or 15 and just stays like that... oddly enough the title I was expecting to get on my graph some how appears in the command window even though I suppressed the output...

I'm hoping someone can explain what I'm doing wrong.
 
Last edited:
Physics news on Phys.org
  • #2
SolutionThe code you provided should work, but the title command needs to be modified slightly. You can use the sprintf() function to format the string for the title command instead of the fprintf() function. The syntax for the title command should look like this:title(sprintf('sin(x-%s)\n',num2str(a)));The output of the sprintf() function is a string that can be used as the argument to the title command. The output of the fprintf() function is intended for printing text to the console window, not for creating a string to use as an argument.
 

1. What is the purpose of animating sin(x-a) in a title?

The purpose of animating sin(x-a) in a title is to visually represent the changes in the function as the value of a changes. This can help viewers understand the behavior of the function and how it changes with different values of a.

2. How is the animation created?

The animation is created using mathematical software or coding languages such as MATLAB, Python, or JavaScript. The function sin(x-a) is graphed and the value of a is changed over time, creating a visual representation of the changing title.

3. What are the benefits of animating sin(x-a) in a title?

Animating sin(x-a) in a title can help viewers better understand the concept of changing values in a function. It can also make the title more visually appealing and engaging.

4. Can this concept be applied to other functions?

Yes, this concept can be applied to other functions. Any function that has a variable that changes over time can be animated in a similar way to demonstrate its behavior.

5. How can animating sin(x-a) be used in scientific research?

Animating sin(x-a) can be used in scientific research to visually represent the behavior of a function that is being studied. This can help researchers better understand and analyze the data, as well as communicate their findings to others in a more engaging way.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Differential Equations
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
567
  • Calculus
Replies
29
Views
713
Replies
3
Views
643
Replies
2
Views
286
Replies
2
Views
1K
Replies
4
Views
345
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
Back
Top