This quiz is about branching, if blocks and functions in MATLAB.





1.  Describe, with an example MATLAB script, the difference between * and .* operators (basically what is the role of . in the later operator? (Hint: describe your answer by multiplying two matrices with each other.)



2.  Vector Dot-Product Vector dot-product in algebra is defined as the summation of the product of the elements of two vectors of the same size. For example,

where $\sum$ denotes summation notation and $n$ is the number of elements of the vectors. Now, given your current knowledge of mathematical operators in MATLAB, what is the simplest way to get the result of the following dot-product of the two vectors $A$ and $B$,



3.  (A) Write a MATLAB function named testString that takes as input, a string and then checks if the input string by the user is the equivalent to string 'MATLAB'. If it is the correct string, then returns logical value true as the function output, otherwise it returns false. Provide two tests of your function which result in true and false output.

(B) One can argue that there are two basic ways of comparing two strings in MATLAB. one is using == operator, and the other is with the use of a MATLAB built-in function that we have repeatedly used in our lectures and homework. What is this function, and what is its difference with == operator. If you don’t remember this function’s name, then at least you should be able to explain the major flaw with string comparison using ==. Explain it with an example MATLAB script.



4.  If you recall from the lectures, MATLAB has two operators corresponding to logical operator OR: | and ||. There are two minor differences between the two if you recall from our discussions in class. One is about code efficiency, and the other relates to whether we are operating on logical vectors or scalars. Describe which operator improves code efficiency and why. For the second difference, you could also provide an example to describe what each operator does.



5.  Recall that in MATLAB true and false are equivalent to 0 and 1,

>> true == 1
ans =
     1
>> false == 0
ans =
     1


Now knowing this, consider the following MATLAB calculation,

>> a = 1;
>> b = 0;
>> x = a/b>10.0
x =
     1


Note that $1/0$ is mathematically undefined (but MATLAB by convention, assumes that $1/0\equiv\infty$). Suppose MATLAB’s default behavior is not what you would like to get, that is, you don’t want $x=1$ when $b=0$, but instead $x=0$. What would be the simplest change to the logical expression x=a/b>10.0 such that the result is $x=0$ whenever $b=0$.



6.  Array of Structures vs. Structures of Arrays. Suppose you would like to store information about students in a class. You create a array of structures like the following,

>> AOS(1).grade = 20;
>> AOS(1).name = 'foobar1';
>> AOS(1).grade = 20;
>> AOS(1).name = 'foobar2';
>> AOS(1).grade = 30;
>> AOS(1).name = 'foobar1';
>> AOS(1).grade = 90;
>> AOS(1).name = 'foobar2';
>> AOS(1).name = 'foobar1';
>> AOS(1).grade = 90;
>> AOS(2).name = 'foobar2';
>> AOS(2).grade = 100;
>> AOS(3).name = 'foobar3';
>> AOS(3).grade = 85;
>> AOS
AOS = 
1x3 struct array with fields:
    name
    grade


Alternatively, you could create a structure array for the same data, like the following,

>> SOA.names = {'foobar1','foobar2','foobar3'};
>> SOA.grades = [90,100,85];
>> SOA
SOA = 
     names: {'foobar1'  'foobar2'  'foobar3'}
    grades: [90 100 85]


Suppose you have 10000 students in your class. Which one of these representations do you think would be more computationally efficient to represent the student data, in order to calculate the average grade in you class? Why? (the explanation is brief, you don’t need to write a paragraph.)



7.  Write a function getRoot() that takes in three coefficients $(a,b,c)$ of a quadratic equation $ax^2+bx+c$ and outputs the roots of this equation as the function output result.



8.  Write a MATLAB anonymous function that calculates the following integral, for arbitrary input integration limits $[a,b]$ and coefficient $c$,

Write your function such that it takes the three variables collectively as a structure.



9.  Write a function getFac() that calculates the factorial of an input integer $n$ (no need for robust coding, assume the input variable is indeed a positive integer).



Comments