Octave:Questions

From AIMSWiki

This section is intended to give you practice answering and coming up with your own practice questions in Octave.

Table of contents

Easy

  • Plot a sine wave of frequency 5 Hz. On the same graph, plot a sine wave of frequency 10 Hz.
    x = linspace(0,1,1000);
    y = sin(x*2*pi*5);
    plot (x,y);
    hold on
    z = sin(x*2*pi*10);
    plot (x,z)
    
  • Use Octave to create matrices

    Ax=[0,1;1,0] Ay=[0,-i;i,0] Az=[1,0;0-1]

     D=Ax*Ay;
     C=Ay*Ax;
     E=Ax*Ax;
     F=Ay*Ay;
     A=E+E;
     B=F+F;
     disp(F);
     T=Ax*Ay+Ay*Ax;
     disp(T)
     F=Ay*Ay;
     K=E+F;
    disp(K)
    


    σx =  =  0 1
    1 0
    σy =  =  0 -i
    i 0
    σz =  =  1 0
    0 -1

    Show that σiσj + σjσi = 2δij.

    ANSWER HERE
    

Intermediate

Structuring programs

  • Write a program that sums the squares of the first n integers.
    ANSWER HERE (or link to answer!)
    
    sum=0
    for i=1 :10
    sum=sum+1
    endfor
    sum
    
    HOW ABOUT THIS FOR AN ALTERNATIVE:
    
    fflush (stdout);
    n = input ("Type a value for n: ")
    nn = 1;
    sum = 0;
    while (nn < n)
    	sum =  sum + nn^2;
    	A(nn) = nn^2;
    	nn++;
    endwhile
    disp ( "The sum of all squares of the first n integers is: "), disp(sum);
    disp ( "The sum of: "), disp(A);
    
    
    
  • How many 1's are there in this (http://www.aims.ac.za/~mackay/compress/filep.01.10000) file? How many zeroes? Write an octave .m file that will figure this out for you.
    ANSWER HERE (or link to answer)
    
    A = load filep.01.10000;
    z = 0;
    o = 0;
    for x = 1:size(A)
    	if A(x) == 0
    		z++;
    	else
    		o++;
    	endif
    endfor
    disp ("The number of zeros is: "); disp (z);
    disp ("The number of ones is: "); disp (o);
    
    
  • Write an .m file that simulates the tossing of a coin. After 10 tosses, how many heads do you get? After 100 tosses? After 100,000 tosses?
    ANSWER HERE
    
  • In epidemiology, the class of people who are not yet infected with a particular disease is called S, the susceptible class, while the class of infected people is denoted I. Let β be the rate at which people catch the disease and γ be the rate at which they recover. A very simple model for infected and susceptible classes is then
    s(t) = − βsi
    i(t) = βsi − γi
    where s and i are the fractions of the total population in each class. Write an octave script file that plots s and t as functions of time for different values of β and γ.
    ANSWER HERE (or link to answer!)
    

File input and output

  • Write an octave program that will read in a matrix from a file, multiply it by its transpose, and write the result to another file.
  • Repeat the above question, but make your .m file executable from the Linux command line and use Linux file redirection to input and output your answer.

Advanced

  • Take this JPG image Image:Penguins.jpgand load it into octave. Set the colormap properly and view the image.
    ANSWER HERE
    
  • Fourier-transform the above image and view its magnitude and phase. What frequencies are the most prominent? Now, add random noise in the frequency domain and transform the image back. Are your changes reflected in the image?
    ANSWER HERE
    
  • Write an .m file that compresses this (http://www.aims.ac.za/~mackay/compress/filep.01.10000) binary file. What degree of compression can you achieve?
    ANSWER HERE