没有合适的资源?快使用搜索试试~ 我知道了~
MATLAB-tutorial-2021.pdf
0 下载量 124 浏览量
2024-07-03
10:44:56
上传
评论
收藏 1.15MB PDF 举报
温馨提示
![preview](https://dl-preview.csdnimg.cn/89510518/0001-c8c9b511f08692c02ec93b109593d031_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
试读
50页
MATLAB_tutorial_2021
资源推荐
资源详情
资源评论
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/release/download_crawler_static/89510518/bg1.jpg)
Dynamics and Vibrations
MATLAB tutorial
School of Engineering
Brown University
To prepare for HW1, do sections 1-11.6 – you can do the rest later as needed
1. What is MATLAB
2. Starting MATLAB
3. Basic MATLAB windows
4. Using the MATLAB command window
5. MATLAB help
6. MATLAB ‘Live Scripts’ (for algebra, plotting, calculus, and solving differential equations exactly)
6.1Solving Equations
6.2 Functions and Plotting
6.3 Calculus
6.4Vectors
6.5 Matrices
6.6 Solving differential equations (with symbols)
7. Working with M-files
8. MATLAB Functions in M files
9. Basic Programming Concepts
9.1 Loops
9.2 Creating a vector with a loop
9.3 Dot notation for operations on vectors
9.4 Operations on vectors using a loop
9.5 Creating matrices with nested loops
9.6 How to find the number of rows and columns in a matrix
9.7 Using plots to display curves and surfaces stored in vectors and matrices
9.8 Conditional statements
10. Organizing complex calculations as functions in an M-file
11. Solving ordinary differential equations (ODEs) using MATLAB
11.1 Solving a basic differential equation
11.2 Solving a basic differential equation in an M-file
11.3 Solving a differential equation with adjustable parameters
11.4 Common errors
11.5 Solving simultaneous differential equations
11.6 Controlling the accuracy of solutions to differential equations
11.7 Looking for special events in a solution
11.8 How the ODE solver works
11.9 Other MATLAB differential equation solvers
12. Using MATLAB solvers and optimizers to make design decisions
12.1 Using fzero to solve equations
12.2 Simple unconstrained optimization problem
12.3 Optimizing with constraints
13. Reading and writing data to/from files
14. Movies and animation
![](https://csdnimg.cn/release/download_crawler_static/89510518/bg2.jpg)
1. What is MATLAB?
You can think of MATLAB as a sort of graphing calculator on steroids – it is designed to help you
manipulate very large sets of numbers quickly and with minimal programming. MATLAB is particularly
good at doing matrix operations (this is the origin of its name). It is also capable of doing symbolic
computations, and has huge numbers of built-in packages that will do things like image processing,
design control systems, machine learning, bioinformatics, and so on. It is also capable of communicating
with a lot of hardware –for example you can use it to program or communicate with Arduinos.
2. Starting MATLAB
MATLAB is installed on the engineering instructional facility. You can find it in the Start>Programs
menu. You can also install MATLAB on your own computer. This is a somewhat involved process – you
need to first register your name at mathworks, then wait until they create an account for you there, then
download MATLAB and activate it. Detailed instructions can be found at
https://www.brown.edu/information-technology/software/catalog/matlab
The instructions tell you to
wait for an email from mathworks, but they don’t always send one. Just check your account – if the
download button for MATLAB appears you are all set. If you have previously registered, you can
download upgraded versions of MATLAB whenever you like. The latest release is 2019b, but if you are
using 2019a that should work fine.
3. Basic MATLAB windows
Install and start MATLAB. You should see the GUI shown below. The various windows may be
positioned differently on your version of MATLAB – they are ‘drag and drop’ windows. You may also
see a slightly different looking GUI if you are using an older version of MATLAB.
Select the directory where
you will load or save files here
Enter basic MATLAB
commands here
Lists variables and
their contents
Lists available
files
Gives details of
functions in your file
Select a convenient directory where you will be able to save your files.
![](https://csdnimg.cn/release/download_crawler_static/89510518/bg3.jpg)
4. Using the MATLAB command window
You can use the MATLAB command window as a simple calculator. Try this for yourself, by typing the
following into the command window. Press ‘enter’ at the end of each line.
>>x=4
>>y=x^2
>>z=factorial(y)
>>w=log(z)*1.e-05
>>sin(pi)
MATLAB will display the solution to each step of the calculation just below the command. Do you notice
anything strange about the solution given to the last step?
We almost never use MATLAB like this, however – the format of the output is difficult to read, and it’s
hard to keep track of complicated calculations. Instead, we write a script to do the calculations, and then
have MATLAB execute the script. There are two general types of MATLAB script:
• ‘Live Scripts,’ which display the results of your calculations, and are good for doing algebra,
calculus, plotting functions, and so on;
• ‘Scripts’ or ‘m files’ which store MATLAB functions – these are usually used for programming,
and can also be easier to use than a ‘Live Script’ when we want to do calculations with numbers
(data processing, simple programming, and in ENGN40, finding numerical solutions to
differential equations).
We will show how to use both type of script in this tutorial.
5. MATLAB help
Help is available through the online manual – Click on the question-mark in the strip near the top right of
the window). The matlab manuals are also online, and will come up in google search.
If you already know the name of the MATLAB function you want to use the help manual is quite good –
you can just enter the name of the function in the search, and a page with a good number of examples
usually comes up. It is more challenging to find out how to do something, but most of the functions you
need can be found by clicking on the MATLAB link on the main page and then following the menus that
come up on subsequent pages.
![](https://csdnimg.cn/release/download_crawler_static/89510518/bg4.jpg)
6. MATLAB ‘Live Scripts’
If you would like to use MATLAB to do math calculations, algebra, or to plot
graphs, you can write a MATLAB ‘Live Script.’ This will organize your
calculations for you, and will also display the results in a nice clean format.
Create a new ‘Live Script’ using the New> Live Script… menu on the top left hand
corner of the MATLAB window.
You should see a menu like the one below.
In the next few sections we will work through some useful things you can do with a ‘Live Script.’
6.1 Solving Equations
Try typing this into the rectangular box:
syms x y
eq1 = x + y == 5
eq2 = x - y == -5
[x,y] = solve([eq1,eq2],[x,y])
Then press the ‘Run All’ arrow at the top of the window to execute the scipt. Note that:
• When you use MATLAB to do algebra (with symbols instead of numbers) you have to specify
that any variables that appear in your equations are ‘syms.’ This is a bit annoying, but you will
get used to it.
•
Notice the two different uses of =. The ‘eq1=’ creates a MATLAB object called eq1 that stores
the first equation. The object can be used in future calculations. If you want to create an
equality inside an equation you have to use = =
Now try (just edit your script; you don’t have to type it all in again)
Enter commands here
Results will appear here
Use this to make results
appear below input
This tab will open more advanced text editing tools
Execute your code
![](https://csdnimg.cn/release/download_crawler_static/89510518/bg5.jpg)
syms x y a b
eq1 = x + y == a;
eq2 = x - y == -b;
solution = solve([eq1,eq2],[x,y])
then run the script again with the green arrow. Note that:
• Putting a semicolon at the end of a line suppresses the output. We usually hide intermediate steps
in calculations to make the script more readable.
• The result of this calculation looks a bit funny. The ‘solution’ variable is a MATLAB object,
and you can access its contents with
solution.x
solution.y
(just add these lines to the end of your script and press Run All again).
Now do a more complicated equation:
syms x y a b z
eq1 = x^2 + y^2 == a;
eq2 = x - y == b;
[x,y] = solve([eq1,eq2],[x,y])
x(1)
x(2)
z = x(1)-y(1)
Notes:
• There are now two solutions, which are returned as vectors. The rows of the vectors match up –
so the first entry in x and the first entry in y is one solution; and the second also go together
• You can extract the first solution in the vectors using x(1), y(1), and the second with x(2),y(2).
• You can use the results of one calculation in a subsequent one.
Now try
syms x y a b z
eq1 = x^2 + y^2 == a;
eq2 = x - y == b;
[x,y] = solve([eq1,eq2],[x,y]);
z = dot(x,y)
simplify(z)
This has calculated the dot product of the two vectors x and y. In some versions of MATLAB, the results
may look funny – if you see bars on top of the variables, they denote complex conjugates, and the | |
denote taking the magnitude of a complex number. If you are using MATLAB 2017b this behavior
won’t show up.
If you get complex numbers you can tell MATLAB that some of your variables are real
syms x y a b z
assume(a,'real')
assume(b,'real')
eq1 = x^2 + y^2 == a;
eq2 = x - y == b;
[x,y] = solve([eq1,eq2],[x,y])
z = dot(x,y)
simplify(z)
剩余49页未读,继续阅读
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
colin工作室
- 粉丝: 983
- 资源: 217
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)