EE185

From High Speed Systems Engineering Lab
Jump to: navigation, search

This is the page for EE185.

History

This is the history.

Lecture Items

This section has interesting items related to lecture.

Vectors

This is the vectors section

Trigonometry

This is the trig section

Complex Numbers

This is the complex numbers section

Lab Items

This section has interesting items related to lab.

MATLAB

Here are some MATLAB notes.

How to use a for-loop

Here is an example of how to use a for-loop (needing explanation...)

 for i = 1:100
   disp(i);
 end

If/Else Statements

If/else statements are a common fixture in programming. They are used to check whether something is true or false. The 'something' that you are checking is called a condition. You probably use if/else statements everyday in decision making without even thinking about it. When you got up before school this morning you may have asked yourself if you were hungry. This would be your condition, the thing to check. If you were hungry, your condition was true! You probably then ate breakfast. Otherwise (else) you weren't hungry and your condition was false and you didn't eat breakfast. If you were to make a MATLAB script out of this type of logical thinking it would look like:

 if amIHungry == true
     eat_breakfast;
 else
     dont_eat_breakfast;
 end

In general if/else statements take the form of:

 if condition_is_true
   (execute code)
 else
   (execute different code)
 end

Here is one more example that uses the 'clock' function in MATLAB. The clock function returns a six element array containing the year, month, day, hour, minute, and seconds in that order. I want to check if the hour element (the fourth element in the array) is after 12 pm(noon). If it is, print a greeting saying good afternoon/evening. If it is before noon, print a greeting saying good morning.

 dateTime = clock;
 hour = dateTime(4);
 if hour >= 12
     disp('Good afternoon/evening!');
 else
     disp('Good morning!');
 end

Nested if/else statements

Nested if/else statements are if/else statements within another if/else statement. They are called nested because they are 'nested' (they reside) within another if/else statement. They can be useful if you are trying to check a condition within another checked condition that was true.

The script below asks a user for a number. The first 'if' checks if the number is less than 0. If the number is the script prints that the number is negative to the console. If the number is not less than 0, then it is either 0 or greater than 0. There is a nested if/else block within the else block. The nested if/else block is called the inner if/else block. The unnested else statement is called the outer else block.

 userInput = input('Enter a number.');
 if userInput < 0
    fprintf('%d is a negative number.\n', userInput);
 else
    if userInput == 0
       fprintf('%d is neither positve or negative.\n', userInput);
    else
       fprintf('%d is a positive number.\n', userInput);
    end
 end

Here is another script that asks a user for a number. The script will then check whether or not the number the user enters is divisible by 2, divisible by 3, divisible by both, or divisible by neither. The script checks this using the 'mod()' function. Mod is short for modulus which returns the remainder when a number is divided by another number. If the remainder is 0, then the first number is divisible by the second number.

 userInput = input('Enter a number: ');
 if mod(userInput, 2) == 0
     if mod(userInput, 3) == 0
         fprintf('%d is divisible by both 2 and 3\n', userInput)
     else
         fprintf('%d is only divisible by 2\n', userInput)
     end
 elseif mod(userInput, 3) == 0
     fprintf('%d is only divisible by 3\n', userInput)
 else
     fprintf('%d is not divisible by 2 or 3\n', userInput)
 end

Nesting if/else statements is okay when you only have a couple conditions to check. If you have more than a few conditions though, nesting if/else blocks can get confusing really fast. Often there is a better solution than nesting. The code below achieves the same thing as the code above but uses 'elseif' to check conditions instead of nested if else statements. Generally this approach looks cleaner and is easier for other people to read.

 userInput = input('Enter a number: ');
 if mod(userInput, 2) == 0 && mod(userInput, 3) == 0
     fprintf('%d is divisible by both 2 and 3\n', userInput)
 elseif mod(userInput, 3) == 0
     fprintf('%d is only divisible by 3\n', userInput)
 elseif mod(userInput, 2) == 0
     fprintf('%d is only divisible by 2\n', userInput)
 else
     fprintf('%d is not divisible by 2 or 3\n', userInput)
 end

Nested for loops

 for i = 1:10
     for j = 1:10
         result = i * j;
         fprintf('%d\t', result)
     end
     disp(' ')
 end

Example.png

Hands-On

Here are some hands-on lab notes.