Tuesday
September 23, 2014

Homework Help: Computer Science - MATLAB

Posted by Lee on Tuesday, July 19, 2011 at 8:40pm.

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.

Answer this Question

First Name:
School Subject:
Answer:

Related Questions

Computer Science - MATLAB - Thanks MathMate. Print plotting sounds awful O_O. ...
Calculus - Consider the function f(x)=sin(1/x) Find a sequence of x-values that ...
Calculus - Consider the function f(x)=sin(1/x) Find a sequence of x-values that ...
Calculus - Consider the function f(x)=sin(1/x) Find a sequence of x-values that ...
math - Eliminate the parameter (What does that mean?) and write a rectangular ...
algebra - Can someone please help me do this problem? That would be great! ...
math - find all solutions in the interval [0,2 pi) sin(x+(3.14/3) + sin(x- 3.14/...
tigonometry - expres the following as sums and differences of sines or cosines ...
Computer Science - MATLAB - Create a stacked contour plot of the MRI data, ...
TRIG! - Posted by hayden on Monday, February 23, 2009 at 4:05pm. sin^6 x + cos^6...

Search
Members