Introduction to Simulation: Beginner


Lab Report

Individual Report
Due date: 2 Mar 2024 (SAT), 11:59
Include: Code, figures, and anything you think it is important :)
Submit: PDF file only to Blackboard
email: fangyuan.wang@connect.polyu.hk (WANG Fangyuan)
Question 1

Matrix P is given by P = [1 2 3 4; 5 6 7 8; 9 10 11 12], write matlab code to calculate the following:

  • Extract the 1st and 3rd row of matrix P and store them in A and B, respectively.
  • Compute the element-by-element multiplication of A and B.
  • Compute the inner products of A and B.
  • Given a linear equation Px=b, where x is a vector of variables we want to solve, and b = [1 0 -1]. Compute x to solve this linear equation.

Question 2

Consider the vector A = [12, 7, 15, 22, 17, 2, 27, 30, 9, 44] .

  • Write a loop that prints each element of A if it is an even number.
  • Write a loop that calculates the sum of the elements in A.
  • Modify the loop you write above to exit if the sum exceeds 100 and print the index at which this happens.
  • Write if-else statement in a loop to do the following for each element in A: if the element is less than 10, print "small"; if the element is between 10 and 25, print "medium"; if it's greater than 25, print "large".

Question 3

In an experimental physics lab, students are studying the relationship between temperature and the resistance of two types of materials, Material X and Material Y. The data they collected is as follows:


Temperature = [20, 50, 100, 150, 200, 250, 300]; % in degrees Celsius
Resistance_of_Material_X = [15, 30, 45, 60, 75, 90, 105]; % in ohms
Resistance_of_Material_Y = [10, 20, 40, 70, 110, 160, 220]; % in ohms
Plot a line graph of Resistance of Material X and Resistance of Material Y in terms of Temperature. And also add proper labels, legend and title to the graph. Requirements:
  • Label the x-axis as "Temperature (°C)" and the y-axis as "Resistance (Ohms)".
  • Add a legend to distinguish between the two materials.
  • Add the title "Temperature vs Resistance for Material X and Y".
  • Use different colors or line styles to distinguish between the two materials.

Should have something similar to this:

Question 4

Linearising a nonlinear function
To obtain a linear model of a nonlinear system, we assume the variables deviate slightly from an operating condition.
Taylor Series: a series expansion of a function about a point
Taylor Series: linear approximation
The linear approximation of the system (function) can be obtained using the tangent line.

f(x) f(x0) + f'(x0)(x - x0)


Nonlinear function: f(x) = x3
Linearise it at x0 = 2 and x0 = -2

Plot the nonlinear function and the linearised functions.
Example:

Question 5

The definition of function f(x,y) with two variables is as follows:

Write Matlab code to compute the result of the function f when (x,y) is given as follows: (-1, -2), (-1, 2), (1, -2) and (1, 2).