Computer Science - MATLAB

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

pause
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.

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)));
pause
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.

  1. 👍 0
  2. 👎 0
  3. 👁 46
asked by Lee
  1. also note that i don't have pause there at the end of my loop i just added that in there when i was trying to slow down the animation to see if something was wrong but nothing was found

    1. 👍 0
    2. 👎 0
    posted by Lee
  2. I have not checked in great detail your code, but something does not look right to me, and it could be the culprit.

    The third line from the end reads:

    title(fprintf('sin(x-%s)\n',num2str(a)));
    Since fprintf() takes at least 3 arguments:
    1. file handle
    2. formatting string
    3. list of arguments

    This is not the one to use.
    You may have meant to use sprintf which formats an argument through the format string and returns a string.

    If I were you, I would replace the original statement to:
    title(sprintf('sin(x-%.2f)\n',a))
    since a is numeric, a float conversion should do the job.

    I would also replace your first "title" statement with the same line, eliminating the second one, since a=0 at the beginning.

    Check if it works out, and let us know what you got.

    1. 👍 0
    2. 👎 0

Respond to this Question

First Name

Your Response

Similar Questions

  1. Computer Science - MATLAB

    Thanks MathMate. Print plotting sounds awful O_O. What you suggested was correct and this code worked disp('Problem 13.9') x=[-2*pi:.00001*pi:2*pi]; a=0; y=sin(x-a); g=plot(x,y); title(sprintf('sin(x-%.2f)\n',a)) grid on;

    asked by Lee on July 20, 2011
  2. Calculus

    Use a Riemann sum with n = 3 terms and the right endpoint rule to approx. ∫(1, 2) sin(1/x)dx. My teacher just needs the terms written out, no need to add or multiply. This is a problem she did up on the board, so here's her

    asked by Justin on November 4, 2015
  3. Powerpoint #3

    okay, I need to do a powerpoint for this career "computer animation" I need help writing a simple, short, punchy paragraph or list. Yet able to get the message across to the audience. Heres the question I have to answer: What is a

    asked by Larry on December 13, 2008
  4. math

    Eliminate the parameter (What does that mean?) and write a rectangular equation for (could it be [t^2 + 3][2t]?) x= t^2 + 3 y = 2t Without a calculator (how can I do that?), determine the exact value of each expression. cos(Sin^-1

    asked by Anonymous on August 3, 2007
  5. Calculus

    Consider the function f(x)=sin(1/x) Find a sequence of x-values that approach 0 such that (1) sin (1/x)=0 {Hint: Use the fact that sin(pi) = sin(2pi)=sin(3pi)=...=sin(npi)=0} (2) sin (1/x)=1 {Hint: Use the fact that sin(npi)/2)=1

    asked by George on September 9, 2008
  6. Calculus

    Consider the function f(x)=sin(1/x) Find a sequence of x-values that approach 0 such that (1) sin (1/x)=0 {Hint: Use the fact that sin(pi) = sin(2pi)=sin(3pi)=...=sin(npi)=0} (2) sin (1/x)=1 {Hint: Use the fact that sin(npi)/2)=1

    asked by George on September 9, 2008
  7. Calculus

    Consider the function f(x)=sin(1/x) Find a sequence of x-values that approach 0 such that sin (1/x)=0 sin (1/x)=1 sin (1/x)=-1 Is sin sin (1/x)=0 and sin (1/x)=-1 does not exist. What is sin (1/x)=1 then.

    asked by George on September 8, 2008
  8. Calculus

    Hello, Could somebody kindly check my answer for the following question? Find the derivative of the following function: h(x)=3e^(sin(x+2)) h'(x)=3'(e^(sin(x+2))+3(e^(sin(x+2))' h'(x)=0(e^(sin(x+2))+3(e^(sin(x+2))(cos(1))

    asked by Constantine on August 18, 2015
  9. algebra

    Can someone please help me do this problem? That would be great! Simplify the expression: sin theta + cos theta * cot theta I'll use A for theta. Cot A = sin A / cos A Therefore: sin A + (cos A * sin A / cos A) = sin A + sin A = 2

    asked by Valerie on February 18, 2007
  10. math

    Create sketches by hand of the following sine functions, showing at least one full period and state the domain and range. Submit them to your teacher using the drop box above. It may be easiest for you to scan your hand drawn

    asked by james on April 25, 2016

More Similar Questions