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

  1. 👍 0
  2. 👎 0
  3. 👁 58
asked by Lee
  1. 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.

    1. 👍 0
    2. 👎 0
  2. 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...

    1. 👍 0
    2. 👎 0
    posted by Lee
  3. 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.

    1. 👍 0
    2. 👎 0
  4. Same thing. I wounder if I'm suppose to be using the surfz function and not some other 3 dimensional plotting function?

    1. 👍 0
    2. 👎 0
    posted by Lee
  5. 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

    1. 👍 0
    2. 👎 0
    posted by Lee
  6. Great! I missed that too (the dot).

    1. 👍 0
    2. 👎 0

Respond to this Question

First Name

Your Response

Similar Questions

  1. programming

    Week 3 - Expand Pseudo Code The accounting office indicated the code must be able to handle overtime hours. Add functionality to your pseudo code so that if more than 40 hours are worked, the hours over 40 receive time and a half

    asked by John on February 22, 2010
  2. discrete math

    Q 7: What is the number of `Hello's printed by the pseudo code below? (for i from lo to hi exhaust i between lo and hi inclusive, and is a empty loop when lo is greater than hi) (a) for i from 1 to n for j from 1 to i - 1 for k

    asked by akash on May 1, 2017
  3. computer

    A-Asking the user to pick a number between 1 and 20 and having the computer pick a number between 1 and 20 and telling the user if they won or lost B- For finding the logic error in the program C- For finding and fixing the logic

    asked by ami on March 11, 2011
  4. Ethics and the Administration of Justice

    Please Help!!!· In previous weeks, you examined the professional code of ethics for a field in criminal justice and analyzed ethical dilemmas for several fields. How does your code of ethics compare to the professional code of

    asked by Everett Douglas on January 13, 2013
  5. ethics

    Write a 1,750- to 2,450-word paper that formulates your personal code of ethics, using one of the major ethical frameworks as a guide. Include answers to the following questions: o In previous weeks, you examined the professional

    asked by jessie on May 15, 2010
  6. English

    1. I move that a vote be taken on this. 2. I suggest that a vote be taken on this. (Does #1 mean #2?) 3. He suggested that she was a professor. (Is this sentence correct? What does 'suggested' mean in this sentence? What other

    asked by rfvv on June 23, 2016
  7. French response

    Mathmate, I wrote a response below. The worksheet I worked on was a crossword puzzle and question 18 is only a five letter word and i'm not sure what it may be.

    asked by Lisa on August 31, 2009
  8. English

    I wrote, "I like to swim in summer." My teacher marked it wrong and said it should read "I like to swim in THE summer." Can anyone offer an explanation, please, why my way is wrong? I don't know why it was marked wrong. Both with

    asked by Charlie on April 17, 2007
  9. Computers - Programming Language

    Convert Algorithm into basic python code using correct syntax: 1) get hours worked 2) get pay rate 3) if hours worked < 40 3.1) gross pay = pay rate times hours worked 4) else 4.1) gross pay = pay rate times 40 plus 1.5 times pay

    asked by anonymous on August 7, 2018
  10. Pseudocode with sequential, selection and arrays

    Analyze, design, and document a simple program that utilizes a good design process and incorporates sequential, selection and repetitive programming statements as well as at least one function call and the use of at least one

    asked by Anonymous on March 9, 2012

More Similar Questions