Questions and Answers

Question 1: How to do questions 4 and 5?
Question 2: What's wrong in my code about mod?
Question 3: What's wrong in my code about sum?
Question 4: What does the %g and %s represent?
Question 5: What's wrong in my code about finding a value of N so that the sum is close to pi/4?
Question 6: What would you suggest for my code about while loop?
Question 7: What's wrong in my code about matrix?

Question 1: I was working on the assignment and I see that I really have no idea how to do questions 4 and 5 in Lecture 2.
Question 4: Given a ``permutation'' vector as y is in the previous item. Find the vector which gives the inverse of the permutation!
Question 5: Given two permutations p and q, find the permutations that is the same as p(q(s)) (that is it permutes the set s as if you would first permute with q and then with p).

Answer 1: (HTML)

Question 2: I am having a little bit of trouble with the second exercise in Lecture 4: Write a little program that checks if x=73 is prime. Do not use isprime. But you might find mod or rem useful.

I came up with this:

for x=[2 3 5 7 11 13 19]
if mod(73,x)=0

I thought this would work, but I get

??? Error: File: primenumbers.m Line: 2 Column: 17
The expression to the left of the equals sign is not a valid target for an assignment.

I am not sure about what I am doing wrong. Could you please tell me?

Answer 2: (HTML)

Question 3: I can't get the warm-up exercise and hence the final exercise in Lecture 4 to work properly.
Warm-up Exercise: Evaluate the sum of -1^(n+1) / (2n-1) for n from 1 to N where N=100.
Final Exercise: Find the sum of a vector x=[3 5 12 42 67] without using the old tricks...that is, use a for loop and don't use sum.

I haven't had a problem with sum before, but for some reason my syntax is calculating something other than the desired sum. I checked my matrix operators and I can't find anything wrong.  Here's my line:

>> x= [1:2]
x = 1     2
>> sum((-1).^(n+1)/(2.*n-1))
ans = -0.0476

But the answer should be .6667. Can you see what's wrong?

Answer 3: The division needs to be pointwise. so use ./ instead of / .

Question 4: I was running this if loop from your lecture notes in Lecture 4:
x = rand;
s = 'The number %g is %s than 0.5\n';
if x<0.5

Questions: What does the %g and %s represent? What does it say in english? Also are the alphabets random or each signify a different type of parameter?

Question: line 5 - fprintf (s,x, 'less') What does this say in english again? Why is s before x?

Answer 4: s is a "format string" when used as the first argument of fprintf or sprintf it tells the function how to generate the result (sprintf returns the result, while fprintf write the result to the screen, or a file).

In the format string, you can have "place holders" for other input. So the %g is a place holder for a number, and it will be converted to a string in the way that matlab shows numbers. %s is a placeholder for another string. The letters are not random...

So in the fprintf command, I'm saying, format the string s, using x as the first input (which will go where the %g is) and 'less' as the second input (which will go where the %s is).

Question 5: I have problems solving the second question in Warm-up Exercise in Lecture 4: Find a value of N so that the sum is close to pi/4 (with difference < 10^-6).

I have written the following code:

for n=1:10^(-6)

and I had the following error message:

??? Error: File: lesson4ex6.m Line: 4 Column: 3
Expression or statement is incomplete or incorrect.

Answer 5:
1. (line 2) 1:10^(-6) is an empty vector. Be careful with operator precedence.

2. (line 3) Even if that was a vector, it would only be a vector of size 10...and in the loop, n would just be a single number...not what you wanted I think. Perhaps you need another variable...just like my mathematics has two variables, N and n...they are not the same.

3. (line 6) Your "if" statement is incomplete. it must not have a newline after the if.

4. (line 7) sun is not sum. Beware of spelling mistakes.

5. (line 7) Remember that a difference might be positive or just checking sum - pi/4 < tolerance is not need to use absolute value (lookfor it).

6. (line 8) Even if you add the absolute value, I'm not sure why you are breaking at this point. Remember, I want you to add 1, 10, 100, 1000 terms of the series and find the error. how does breaking help you do that?

The result of the calculation should be a vector (lets call it E, for error), of length 7 so that E(i) contains the error resulting from adding up 10^(i-1) terms.

Question 6: I have problem with the exercise in Lecture 5: Write a while loop that modifies n. if n is even it changes n to n/2, otherwise, it changes it to 3n+1. if n is 1 the loop stops. Try this out for several different starting values. Count how many steps you need to get to 1.

I have attached my version here. I can't seem to get it to work. What would you suggest?

function y = counter(x)
while x>1
counter1=counter1 + 1;
if mod(x,2)==0
y = counter1-1

The below program is functional, but I don't know how to turn it into a function.

while n>1
counter=counter + 1
if mod(n,2)==0
fprintf('the n is %g', counter-1)

Answer 6: 2 comments.

1. You have an empty line as the first line of the code. The very first line must have the "function y = ...." code, or a comment (starting with %). No empty lines allowed before the keyword function...This might be your only problem.

2. You can put the y=counter1-1 outside the while loop. No need to make this assignment every iteration..only after you are done.

Question 7: For the additional exercise in Lecture 6:

Create r and c such that:
     ( 1 1 1 1 1 )
     ( 2 2 2 2 2 )
r =  ( 3 3 3 3 3 )     c = r'
     ( 4 4 4 4 4 )
     ( 5 5 5 5 5 )

I tried a couple ways to create the matrix as follows, but some works and some doesn't. Could you tell me what's wrong? The following three ways have worked.

> > x = [1:5; 1:5; 1:5; 1:5; 1:5 ]
x =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
> > x = [ ones(1,5); ones(1,5)*2; ones(1,5)*3; ones(1,5)*4; ones(1,5)*5 ]'
x =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
> > ones(5,1)*[1:5]
ans =
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

However this doesn't work. Why?

>> > ones(5,1) : ones(5,1)*5
ans =
1 2 3 4 5

Also, could you tell me how I can use ( : ) which you suggested after the lecture for me to create the matrix easily ?

Answer 7: The third way you suggested is one way I was thinking about (note that you
can change the 5 to a 100 and it will create a 100x100 matrix...)

But you can also do


The fourth way you did has a colon between two vectors...I'm not sure what
you expect that to do.