====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
gibson:teaching:fall-2013:math445:hw3solns [2013/10/22 19:45] gibson created |
gibson:teaching:fall-2013:math445:hw3solns [2013/10/22 19:51] (current) gibson |
||
---|---|---|---|
Line 88: | Line 88: | ||
15. returns 1 (true) if its argument is divisible by 3 and 0 (false) if it's not. | 15. returns 1 (true) if its argument is divisible by 3 and 0 (false) if it's not. | ||
- | function r = divisiblebythree(n) | + | <code> |
- | % divisiblebythree(n) : return 1 if n is divisible by 3 or 0 if it's not | + | function r = divisiblebythree(n) |
+ | % divisiblebythree(n) : return 1 if n is divisible by 3 or 0 if it's not | ||
- | r = 3*round(n/3) == n | + | r = 3*round(n/3) == n |
| | ||
- | end | + | end |
+ | </code> | ||
16. takes a vector //x// as input and returns 1 if the components of //x// are sorted | 16. takes a vector //x// as input and returns 1 if the components of //x// are sorted | ||
in ascending order, 0 if not. | in ascending order, 0 if not. | ||
- | function s = sorttest(x) | + | <code> |
- | % return 1 if x is sorted in ascending order, 0 if not | + | function s = sorttest(x) |
+ | % return 1 if x is sorted in ascending order, 0 if not | ||
| | ||
- | N = length(x); | + | N = length(x); |
- | % count how many of {x(1) through x(N-1)} are less than {x(2) through x(N)} | + | % count how many of {x(1) through x(N-1)} are less than {x(2) through x(N)} |
- | % the count will be N-1 if the x is sorted in ascending order | + | % the count will be N-1 if the x is sorted in ascending order |
- | s = sum(x(1:N-1) < x(2:N)) == N-1 | + | s = sum(x(1:N-1) < x(2:N)) == N-1 |
- | end | + | end |
+ | </code> | ||
Line 115: | Line 119: | ||
</latex> | </latex> | ||
- | function y = matvecmult(A,x); | + | <code> |
- | % matvecmult: compute matrix-vector multiplication A x | + | function y = matvecmult(A,x); |
+ | % matvecmult: compute matrix-vector multiplication A x | ||
- | [M,N] = size(A); | + | [M,N] = size(A); |
- | % M x N matrix times N-vector equals an M-vector | + | % M x N matrix times N-vector equals an M-vector |
- | y = zeros(M,1); | + | y = zeros(M,1); |
| | ||
- | % evaluate formula for ith component of y | + | % evaluate formula for ith component of y |
- | for i=1:M | + | for i=1:M |
| | ||
- | % evaluate y_i = sum_{j=1}^n A_ij x_j | + | % evaluate y_i = sum_{j=1}^n A_ij x_j |
- | for j=1:N | + | for j=1:N |
- | y(i) = y(i) + A(i,j)*x(j); | + | y(i) = y(i) + A(i,j)*x(j); |
- | end | + | |
end | end | ||
+ | |||
+ | end | ||
| | ||
end | end | ||
+ | </code> | ||
Line 171: | Line 177: | ||
where //x// and //y// range from -10 to 10. Label the axes. | where //x// and //y// range from -10 to 10. Label the axes. | ||
+ | <code> | ||
N = length(x); | N = length(x); | ||
F = zeros(N,N); | F = zeros(N,N); | ||
Line 190: | Line 197: | ||
axis equal | axis equal | ||
axis tight | axis tight | ||
+ | </code> | ||
| | ||
---- | ---- | ||
Line 215: | Line 222: | ||
28. Write a function that computes the factorial of an integer n using a ''while'' loop. | 28. Write a function that computes the factorial of an integer n using a ''while'' loop. | ||
- | function f = factorial2(n) | + | <code> |
- | % factorial2: compute n! | + | function f = factorial2(n) |
+ | % factorial2: compute n! | ||
- | f = 1; | + | f = 1; |
- | while (n>1); | + | while (n>1); |
- | f = f*n; | + | f = f*n; |
- | n = n-1; | + | n = n-1; |
- | end | + | |
end | end | ||
+ | end | ||
+ | </code> | ||
29. Write an ''isPrime(n)'' function that returns 1 (true) is n is prime | 29. Write an ''isPrime(n)'' function that returns 1 (true) is n is prime | ||
Line 229: | Line 238: | ||
or doing it with integer arithmetic. | or doing it with integer arithmetic. | ||
+ | <code> | ||
function r = isprime(n) | function r = isprime(n) | ||
% isprime(n): return 1 (true) of n is prime, 0 (false) if it isn't. (crude!) | % isprime(n): return 1 (true) of n is prime, 0 (false) if it isn't. (crude!) | ||
Line 248: | Line 258: | ||
end | end | ||
end | end | ||
+ | </code> | ||
30. Write a function that returns a vector of all the integer divisors of an integer n. | 30. Write a function that returns a vector of all the integer divisors of an integer n. | ||
Again, don't worry about efficiency or integer arithmetic. | Again, don't worry about efficiency or integer arithmetic. | ||
+ | <code> | ||
function d = divisors(n) | function d = divisors(n) | ||
% divisors(n): return vector of all divisors of n | % divisors(n): return vector of all divisors of n | ||
Line 265: | Line 276: | ||
end | end | ||
end | end | ||
+ | </code> |