WhatsApp Image 2025-03-01 at 15.52.47.jpeg
Document Details

Uploaded by GoodSamarium1263
Université Rennes 2
Full Transcript
# Lab 1: Introduction to MATLAB ## Objectives After completing this lab, you should be able to: * Start and exit the MATLAB program * Use MATLAB as a calculator * Use the help command * Assign values to variables * Use some of MATLAB's built-in functions * Create and run a MATLAB scri...
# Lab 1: Introduction to MATLAB ## Objectives After completing this lab, you should be able to: * Start and exit the MATLAB program * Use MATLAB as a calculator * Use the help command * Assign values to variables * Use some of MATLAB's built-in functions * Create and run a MATLAB script file * Create simple plots ## Background MATLAB is a powerful software package that is widely used in engineering, science, and mathematics. It provides a flexible environment for numerical computation, visualization, and programming. ### Starting MATLAB On most systems, you can start MATLAB by double-clicking on the MATLAB icon or by typing `matlab` at the command prompt. When MATLAB starts, a window will appear with a command prompt (`>>`). You can enter commands at the prompt, and MATLAB will execute them. ### MATLAB as a Calculator You can use MATLAB as a calculator by typing expressions at the command prompt. For example, to add two numbers, type: ``` >> 2 + 3 ``` MATLAB will respond with: ``` ans = 5 ``` You can also use other mathematical operators, such as subtraction (`-`), multiplication (`*`), division (`/`), and exponentiation (`^`). For example: ``` >> 2 * 3 + 4 / 2 ans = 8 >> 2 ^ 3 ans = 8 ``` MATLAB follows the standard order of operations (PEMDAS). ### The Help Command MATLAB has a comprehensive help system that can provide information about any command or function. To access the help system, type `help` followed by the name of the command or function. For example, to get help on the `sin` function, type: ``` >> help sin ``` MATLAB will display a description of the `sin` function, its syntax, and examples of how to use it. ### Variables You can assign values to variables in MATLAB using the assignment operator (`=`). For example, to assign the value 5 to the variable `x`, type: ``` >> x = 5 ``` MATLAB will respond with: ``` x = 5 ``` Now, you can use the variable `x` in expressions. For example: ``` >> x + 3 ans = 8 ``` Variable names in MATLAB must begin with a letter and can contain letters, numbers, and underscores. MATLAB is case-sensitive, so `x` and `X` are different variables. ### Built-in Functions MATLAB has a large number of built-in functions that can perform a variety of tasks. Some common functions include: * `sin(x)`: sine of x (in radians) * `cos(x)`: cosine of x (in radians) * `tan(x)`: tangent of x (in radians) * `exp(x)`: exponential of x ($e^x$) * `log(x)`: natural logarithm of x (base e) * `log10(x)`: common logarithm of x (base 10) * `sqrt(x)`: square root of x * `abs(x)`: absolute value of x For example, to calculate the sine of $\pi/2$, type: ``` >> sin(pi/2) ans = 1 ``` ### Script Files A script file is a text file that contains a sequence of MATLAB commands. You can create a script file using any text editor. To run a script file, type its name at the command prompt. For example, if you have a script file named `my_script.m`, you can run it by typing: ``` >> my_script ``` MATLAB will execute the commands in the script file in order. ### Plotting MATLAB has powerful plotting capabilities. The `plot` function can be used to create 2-D plots. For example, to plot the function $y = sin(x)$ from $x = 0$ to $2\pi$, you can use the following commands: ``` >> x = 0:0.1:2*pi; >> y = sin(x); >> plot(x, y) ``` The first line creates a vector `x` containing values from 0 to $2\pi$ with a step size of 0.1. The second line calculates the sine of each value in `x` and stores the result in the vector `y`. The third line creates a plot of `y` versus `x`. ## Lab Exercises 1. **MATLAB as a Calculator:** Use MATLAB to evaluate the following expressions: * $(3 + 4) * 5$ * $10 / (2 + 3)$ * $2^4 - 1$ * $\sqrt{16} + 2 * 3$ 2. **Variables:** Assign the value 10 to the variable `a` and the value 20 to the variable `b`. Then, use MATLAB to evaluate the following expressions: * $a + b$ * $a * b$ * $b / a$ * $a^2 + b^2$ 3. **Built-in Functions:** Use MATLAB to evaluate the following expressions: * $\sin(\pi/4)$ * $\cos(\pi/3)$ * $\tan(\pi/6)$ * $e^2$ * $\log(10)$ * $\log10(100)$ * $\sqrt{25}$ * $|−5|$ 4. **Script Files:** Create a script file named `my_script.m` that contains the following commands: ```matlab % This is a script file x = 1:10; y = x.^2; plot(x, y) title('Plot of y = x^2') xlabel('x') ylabel('y') ``` Run the script file. What does the plot look like? 5. **Plotting:** Create a plot of the function $y = cos(x)$ from $x = -2\pi$ to $2\pi$. Label the axes and add a title to the plot. ## Submission Submit a document containing the following: * Answers to the Lab Exercises, including the MATLAB commands you used and the results you obtained. * The script file `my_script.m` that you created in Exercise 4. * The plot you created in Exercise 5. ## Additional Resources * MATLAB Documentation: `https://www.mathworks.com/help/matlab/` * MATLAB Tutorials: `https://www.mathworks.com/tutorials.html`