21
CS1109 LAB 3 July 3rd

CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

Embed Size (px)

Citation preview

Page 1: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

CS1109 LAB 3July 3rd

Page 2: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

ROAD MAP

Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

Page 3: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

HOMEWORK SUBMISSION

Zip files saving and submitting File name File location Homework 1 questions

Page 4: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

LAST WEEK

Logic operator == && || ~= >= <=

Built in function rand fix, floor, ceil disp, fprintf

Page 5: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

REVIEW EXERCISE

Compute without using Matlab (~0 & 0)||(0&0) ((~1 || 0)& (0&0))||(1&0) (~(0& 1))||(1&1) suppose a = 1, b = 3

if a>1 & b+a>2a = 3

else a= 4

endQuestion: a+b = ?

Page 6: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

REVIEW EXERCISE

suppose a = 1, b = 3if a>1 & b+a>2

a = 3;elseif a> -1

a= 4;else

a = 5;end

Question: a+b = ?

Page 7: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

REVIEW EXERCISE

suppose a = 1, b = 3if a>-1 & b+a>2

a = 3;elseif a> -1

a= 4;else

a = 5;end

Question: a+b = ?

Page 8: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

DOUBLE INEQUALITY

Type in MATLAB z=6 4<=z<=5

Page 9: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

DOUBLE INEQUALITY

Type in MATLAB z=6 4<=z<=5

Something is wrong. If z = 6 then 4<=z<=5 is false. Let's figure out what went wrong later.

First, design a logical statement which will give the right result.

How many ways can you write it?

Page 10: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

DOUBLE INEQUALITY

Page 11: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

DOUBLE INEQUALITY

z<= 5 && z>=4 z<=5 & z>=4 4 <=z && z<=5 4<=z && 5>= z

Page 12: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

THIS WEEK

Scrip and Function If statement While loop For loop Array

Page 13: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

HOW TO USE FUNCTION AND SCRIPTS

function out_var=my function(inp_var)% Comments that explain what the function does

computationsout_var=desired result;

End

Exercise 1: Try to create a function that adds two numbers The function is called addtwo.m Input: x, y Output: z

Page 14: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

function z=addtwo(x,y)% addtwo(x,y) Adds two numbers, vectors,

whatever,% and output the result = x + yz=x+y;

end

Page 15: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

SCRIPT

Scripts are the simplest kind of program file because they have no input or output arguments.

Useful for automating series of commands, such as computations that you have to perform repeatedly from the command line.

Exercise 2: Create a script that asks the user to input

two numbers, then add the two numbers and output the sum.

Page 16: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions
Page 17: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

EXERCISE 4

Create a function called new_abs.m which computes the absolute value of x

Input: x Output: y (y =|x|)

Page 18: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions
Page 19: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

EXERCISE 4

function y=new_abs(x)if x>=0

y=x;

elsey=-x;

end

end

Can you make the above code shorter?

Page 20: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

WHILE

while logical condition do something

end

While loop repeats an indefinite number of times until the logical condition matched.

Exercise:Sum 1 to 100Sum 1 to 100, only the numbers that are divisible

by 3

Page 21: CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions

CHALLENGE QUESTIONS

Write a while loop that generates random integer numbers between 1 and 5 and stops looping after it generates the same number twice.

Write a while loop that generates random integer numbers between 1 and 100 and stops looping after it generates the same number twice. (Use array)