20-CS-110-001 Introduction to Computer Science Fall 2010

## Lab Number 1

Matlab

Due: September 30, 2010
Submit matlab source code via blackboard. See submit page for instructions.

Rationale:
 Your projects will be matlab solutions to some nearly real world problems.

Matlab:
 To open Matlab click "Start", click "Mathematics", then select "Matlab". There are three windows. The window on the right is the console window and is where commands are executed. To edit a file, in Matlab drop the "File" menu, click "New", then select "M-file". A blank (editor) window opens. In that window type v=[1 2 3] and save by dropping the "File" menu in the editor window then clicking "Save As...". A file chooser opens. Where it says "Enter file name:" type "first.m" then click on "Save". Now, in the console window type first. The result is ``` v = 1 2 3 ``` What you have just done is created a simple Matlab program and run it. The output is shown above. In this part of the assignment you will create one Matlab program that solves a lot of problems, listed below, and displays output for each. Call the program first (therefore it is in a file named first.m). There should be a comment before each problem solution in the Matlab code. Comments begin with %. Submit via blackboard using these instructions. At the top of the file add a comment which includes your ucfilespace user name so I can check out your portfolio page. Since there are 15 problems to solve it is best to run the solution to each in the console to make sure it works, then edit the solution into the file first.m. When finished, run first.m to make sure that all the solutions are correct. The list of problems is as follows: Use lines of the form a = input('number> '); to input two numbers a and b and print the product of those numbers. Repeat the above except now each input item is a vector of 5 numbers and the prompt says vector instead of number. The product you should code will multiply elements at the same indices. For example, the product of [1 2 3 4 5] and [6 7 8 9 10] will be the vector [6 14 24 36 50]. Print the product. Make a a column vector. For example, if a is [1 2 3 4 5], change it to ```a = 1 2 3 4 5 ``` Print the column vector. Take the vector product of a and b (in that order from left to right) and b and a (in that order from left to right). Print both products. Print the sum of all the numbers in a+b. Create and print a vector v of 50 numbers, equally far apart between and including 0 and 2π. Print sin(v). Execute plot(v,sin(v)). As for the last problem, produce a plot of sin(1/x) for all x in the vector v. Repeat the problem above with a vector of 2000 numbers from 0.01 to 0.1. Create the vector [1 2 3 4 5 6 7]. Input a vector v of numbers from the console and find and print the smallest number in the vector, also the largest. Use the vector of the last problem. Find the following sum: the first number of v plus 2 times the second number of v plus 3 times the third number of v and so on. You may need to use length(v) which returns the number of numbers in v. For example length([1 2 3 2 1]) is 5. Find the 10th root of 1234567. Use format long. The answer is 4.06585106808254. Find two integers denominator and numerator such that numerator/denominator comes closest to π of all such integer divisions where numerator is less than P where P is 10000. Repeat with P equal to 100000.