MATLAB EXERCISES 3


Write a function that accepts a single 3-element array (e.g. [3 4 5]), and returns a logical value (0 or 1) indicating if the 3 elements of the array represent the sides of a valid triangle. For those who can't remember...in a valid triangle the sum of the lenghts of the 2 shorter sides are greater than the length of the longest side.

Some functions that might be useful (you won't need all of them):

min

max

sort

sum

use the help system to find out how to use them (type 'help sort' or open the helpdesk and search for the function name): There are multiple ways to solve this problem…think of logic first, then implement it.

Two different answers (don't look until you've tried it!): istriangle1.m   istriangle2.m


Write a short function to:

save the function to your current working directory, and run it with different values & numbers of inputs

An answer: quadplot.m


Save the file http://homepages.see.leeds.ac.uk/~lecimb/matlab/code-to-debug/collatz.m  to your current working directory.

collatz(n) applies an iterative algorithm to the integer n to generate a sequence of numbers, finishing when the last number in the sequence equals 1. Try running it for any integer n.

There are 3 bugs in the code – find and correct them. When corrected the code should be able to produce the results below.

collatz(5) -> [5 16 8 4 2 1]

collatz(6) -> [6 3 10 5 16 8 4 2 1]

Answer


Home | IMB's Homepage