CE 2 Midterm

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/50

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 11:14 PM on 4/4/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

51 Terms

1
New cards
Sum of elements

sum_x = sum(x)

2
New cards
Average value

mean_x = mean(x)

3
New cards
Maximum value

max_x = max(x)

4
New cards
Minimum value

min_x = min(x)

5
New cards
 Number of elements

len_x = length(x)

6
New cards

Single Input/Output Function

function [outputNumber] = myFirstFunction(inputNumber) 

7
New cards

What is the correct syntax for defining a function in MATLAB?

function [output1, output2] = functionName(input1, input2)

% Function body

output1 = ...;

output2 = ...;

end

8
New cards

Write a function that takes two inputs and returns division, subtraction, and addition

function [out1, out2, out3] = threeOutputs(in1, in2)
    out1 = in1 / in2;   
    out2 = in1 - in2;     
    out3 = in1 + in2;     
end

9
New cards

What will this function return for different inputs?

function z = functionxyz(x, y)

if x == 0

z = 0;

elseif y == 0

z = 1;

else

z = 2;

end

end

If x = 0: returns 0 (regardless of y)

If x ≠ 0 and y = 0: returns 1

If x ≠ 0 and y ≠ 0: returns 2

10
New cards

Create a function that calculates the area of a rectangle

function area = AreaRECT(l, w)
    area = l * w;
end

11
New cards

Identify the data types in this code:

b = double(a);

row_array = [1,2,2; 3,2,2];

mycharacter = {'a'};

mystring = "myname";

a = 2;

myboolean = true;

double(a) - Double precision number

row_array - 2x3 Matrix/Array

mycharacter - Cell array

mystring - String

a = 2 - Double (default numeric type)

myboolean - Logical/Boolean

12
New cards

How do you calculate the number of seconds in 6.78 days?

day = 24; % hours per day

hr = 60; % minutes per hour

min = 60; % seconds per minute

days = 6.78;

seconds = days*day*hr * min;

13
New cards

Convert 2125 lbf/in² to N/mm² (given: 25.4 mm = 1 in, 4.448 N = 1 lbf)

mm = 25.4;

N = 4.448;

in = 1;

lbf = 1;

result = 2125 (in/mm)^2 (N/lbf);

14
New cards

Write code to perform addition, subtraction, multiplication, and division with a=8, b=5

a = 8; 
b = 5; 

add = a + b;    % 13
sub = a - b;    % 3
mul = a * b;    % 40
dv = a / b;     % 1.6

15
New cards

What are the 4 key rules for writing functions?

  1. Function name MUST match filename

  2. Never assign values to input variables inside function

  3. All outputs must be calculated from inputs

  4. End the function with end keyword

16
New cards

How do you call a function that returns three outputs?

[var1, var2, var3] = functionName(input1, input2);

17
New cards

Logic Operators for And, OR, NOT, Equals, Not Equals

&&, I I, ~, ==, ~=

18
New cards

How to call function

[a,b] = myFunc(x,y)

19
New cards

Boolean Variables

1 = true, 0 = false

20
New cards

Script

File containing sequence of commands

21
New cards

Pseudo Code

Outline logic in English before MATLAB code

22
New cards

Write a function that returns:

  • 0 if x == 0

  • 1 if y == 0

  • 2 otherwise

if x == 0
    z = 0;
elseif y == 0
    z = 1;
else
    z = 2;
end

23
New cards

Command Window

The interactive area where you can enter individual commands and see immediate results. Great for testing, but work isn't saved. Users can enter commands and run scripts.

24
New cards

Workspace

A window that lists all the variables currently stored in memory, showing their names, values, and classes (types).

25
New cards

Editor

Where you write and edit Scripts and Functions. This is where your code is saved as a .m file.

26
New cards

Current Folder

Shows the files in the directory you are currently working in. Your functions must be saved here to be called.

27
New cards

Double

The default for numbers (x = 5.5)

28
New cards

Branching (If/Else)

allows the code to choose a path.

<p>allows the code to choose a path.</p>
29
New cards

Characters

Only letters, numbers, and underscores (_) No spaces, no hyphens, no $ or %

30
New cards

std(x)

Standard Deviation

31
New cards

sqrt(x)

Square root

32
New cards

Row Vector

v = [1, 2, 3] or v = [1 2 3] (commas or spaces)

33
New cards

Column Vector

v = [1; 2; 3] (semicolons)

34
New cards

Matrix

M = [1, 2; 3, 4] (A 2x2 square)

35
New cards

a function that returns a letter grade based on numerical score using elseif

function grade = calculateGrade(score)
    if score >= 90
        grade = 'A';
    elseif score >= 80
        grade = 'B';
    elseif score >= 70
        grade = 'C';
    elseif score >= 60
        grade = 'D';
    else
        grade = 'F';
    end
end

36
New cards

a function that gives advice based on temperature using elseif

function advice = temperatureAdvisory(temp)
    if temp > 100
        advice = 'Extreme heat - stay indoors';
    elseif temp > 80
        advice = 'Hot - stay hydrated';
    elseif temp > 60
        advice = 'Pleasant weather';
    elseif temp > 32
        advice = 'Cool - wear a jacket';
    else
        advice = 'Freezing - stay warm';
    end
end

37
New cards

Write an if statement that checks if a number is between 1 and 10 (inclusive)

if x >= 1 && x <= 10
    disp('x is between 1 and 10');
end

38
New cards

a function that checks if someone is eligible to vote (age ≥ 18 and citizen = true)

function eligible = canVote(age, isCitizen)
    if age >= 18 && isCitizen == true
        eligible = true;
    else
        eligible = false;
    end
end

39
New cards

function that classifies a number as positive, negative, or zero using elseif

function classification = classifyNumber(x)
    if x > 0
        classification = 'Positive';
    elseif x < 0
        classification = 'Negative';
    else
        classification = 'Zero';
    end
end

40
New cards

While-Loop

Loop as long as a condition is true

41
New cards

While Loop Example

n = 1;

nFactorial = 1;

while nFactorial < 1e100 % Conditional Statement

n = n + 1;

nFactorial = nFactorial * n;

end

42
New cards

For Loop

Loop a specific number of times

43
New cards

For Loop Example

for i = 2:3:20 % Loop indexing variable

A(i) = A(i) * x

x = x+1

end

44
New cards

If else example (Size)

if x > 10 % 1st Conditional Statement

disp(“It is size large”)

elseif x >= 5 % 2nd Conditional Statement

disp(“The size is medium”)

else

disp(“The size is small”)

end

45
New cards

Plot(x,y)

will create a line on the figure connecting points, where all of the coordinates come from x and y

46
New cards

xlabel(‘string’) ylabel(‘ string’)

These will label your axes with the string you put in the parentheses 

47
New cards

hold on

Stops your current figure from being overwritten

Will plot new lines and scatter plots in different colors so you can distinguish

them

48
New cards

N=length(array)

for i = 1:N

Will counts through all of the indices in array above from 1 to N

49
New cards

Matrices

Multidimensional arrays, Matrices are the 2-dimensional version of arrays. We store numbers in them

50
New cards

[numRows, numCols] = size(x)

This will tell you how many rows and columns are in any matrix

51
New cards

Explore top flashcards

flashcards
Week 1
20
Updated 716d ago
0.0(0)
flashcards
Introduction to Biology
33
Updated 446d ago
0.0(0)
flashcards
Classical Roots Lessons 7-8
42
Updated 1146d ago
0.0(0)
flashcards
Civil Rights and Liberties
38
Updated 1075d ago
0.0(0)
flashcards
units 1-7 vocab
361
Updated 1081d ago
0.0(0)
flashcards
Survey of Humanities- Boroque
40
Updated 925d ago
0.0(0)
flashcards
Week 1
20
Updated 716d ago
0.0(0)
flashcards
Introduction to Biology
33
Updated 446d ago
0.0(0)
flashcards
Classical Roots Lessons 7-8
42
Updated 1146d ago
0.0(0)
flashcards
Civil Rights and Liberties
38
Updated 1075d ago
0.0(0)
flashcards
units 1-7 vocab
361
Updated 1081d ago
0.0(0)
flashcards
Survey of Humanities- Boroque
40
Updated 925d ago
0.0(0)