Computer Science - MATLAB

posted by .

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;
set(g,'EraseMode','xor')
while a<=8*pi
a=a+.1*pi;
y=sin(x-a);
plot(x,y)
title(fprintf('sin(x-%.2f)\n',a))
grid on;
drawnow
end

I have another question though that I seem to be struggling with.

Create an animation of the following:

Let x vary form -2pi to +2pi
Let y=sin(x)
Let z=sin(x-a)cos(y-a)

Let a be the animation variable.
Remember that you'll need to mesh x and y to create two-dimensional matrices; use the resulting arrays to find z.

Here's my attempt at a solution.

x=-2*pi:pi/50:2*pi;
y=sin(x);
[X,Y]=meshgrid(x,y);
z=sin(X)*cos(Y);
h=surf(z);
axis tight
set(gca,'nextplot','replacechildren');
shading interp
colormap(jet)
for a=0:pi/100:8*pi
z=sin(X-a)*cos(Y-a);
set(h,'zdata',z);
drawnow
end

When I try to run it the axes are colored for like a tenth of a second and then it just turns to white. I don't know exactly what I'm doing wrong.

  • Computer Science - MATLAB -

    I had a quick look at the code, and I suggest the following changes (just a guess).

    I believe the code has worked as expected, but the duration of the animation is only about 1/10th of a second.
    To test the hypothesis, change
    for a=0:pi/100:8*pi
    to
    for a=0:pi/1000:8*pi
    to make 10 times more frames. If it stays for about a second, then it is the problem.

    The other "problem" that it stays white at the end is probably the endpoint (8π) puts everything to zero, so it is/may be coloured white.
    Try changing the endpoint to, say
    8.5π or 8.25π and see if it makes a difference.

    Let me know how it goes.

  • Computer Science - MATLAB -

    I tried both and both results leave me with the same result, It just flashes real quick and then turns white... I even tried a=0:pi/1000000000:8*pi, it stays for a little bit longer but then turns white....

    I think it's an issue with something else... what I'm not exactly sure as it's strange that something does appear but then quickly disappears...

  • Computer Science - MATLAB -

    A couple of things you could try:

    1. put
    refreshdata
    before "drawnow".
    Hopefully this will update every frame of the image.
    2. put a finer grid, such as
    x=-2*pi:pi/500:2*pi;
    instead of 50.

    Sorry that I cannot be more definite because I don't have access to Matlab, especially the plotting part.

  • Computer Science - MATLAB -

    Same thing. I wounder if I'm suppose to be using the surfz function and not some other 3 dimensional plotting function?

  • Computer Science - MATLAB -

    got it
    <_< forgot the dot and got rid of the axis tight

    disp('Problem 13.11')
    x=-2*pi:pi/50:2*pi;
    y=sin(x);
    [X,Y]=meshgrid(x,y);
    z=sin(X)*cos(Y);
    h=surf(z);
    set(gca,'nextplot','replacechildren');
    shading interp
    colormap(jet)
    for a=0:pi/100:8.25*pi
    z=sin(X-a).*cos(Y-a);
    set(h,'zdata',z);
    drawnow
    end

  • Computer Science - MATLAB -

    Great! I missed that too (the dot).

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. tigonometry

    expres the following as sums and differences of sines or cosines cos8t * sin2t sin(a+b) = sin(a)cos(b) + cos(a)sin(b) replacing by by -b and using that cos(-b)= cos(b) sin(-b)= -sin(b) gives: sin(a-b) = sin(a)cos(b) - cos(a)sin(b) …
  2. algebra

    Can someone please help me do this problem?
  3. Mathematics - Trigonometric Identities

    Let y represent theta Prove: 1 + 1/tan^2y = 1/sin^2y My Answer: LS: = 1 + 1/tan^2y = (sin^2y + cos^2y) + 1 /(sin^2y/cos^2y) = (sin^2y + cos^2y) + 1 x (cos^2y/sin^2y) = (sin^2y + cos^2y) + (sin^2y + cos^2y) (cos^2y/sin^2y) = (sin^2y …
  4. 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.
  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 if …
  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 if …
  7. TRIG!

    Posted by hayden on Monday, February 23, 2009 at 4:05pm. sin^6 x + cos^6 x=1 - (3/4)sin^2 2x work on one side only! Responses Trig please help! - Reiny, Monday, February 23, 2009 at 4:27pm LS looks like the sum of cubes sin^6 x + cos^6 …
  8. pre calc trig check my work please

    sin x + cos x -------------- = ? sin x sin x cos x ----- + ----- = sin x sin x cos x/sin x = cot x this is what i got, the problem is we have a match the expression to the equation work sheet and this is not one of the answers. need
  9. 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; …
  10. Trigonometry

    Solve the equation for solutions in the interval 0<=theta<2pi Problem 1. 3cot^2-4csc=1 My attempt: 3(cos^2/sin^2)-4/sin=1 3(cos^2/sin^2) - 4sin/sin^2 = 1 3cos^2 -4sin =sin^2 3cos^2-(1-cos^2) =4sin 4cos^2 -1 =4sin Cos^2 - sin=1/4 …

More Similar Questions