Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 1 MATLAB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Islamic University ‫الجامعة اإلسالمية‬

Faculty of Engineering
Department of Electrical
‫كلية الهندسة‬
Engineering ‫قسم الهندسة الكهربائية‬

ENGR 3032
SCIENTIFIC COMPUTING LANGUAGES
Term Fall 2024

EXPERIMENT 1
Getting started with Scientific Computing Languages
LAB REPORT
[CO 1 ; PI_1_3 ; SO 1 ]

Submitted To
Osamah Alkhalifah

Submitted By
Mohammed Rasheed, 431014033

Section No: 2623

September 7 , 2024

ENGR 3032, Term 443 2023 Page 1 of 8


Objectives:
The objective of this laboratory session is to be familiar with scientific computing languages
such as Matlab and Octave. This lab session will also provide clear understanding about how
to save and restore scripts (m files).

Theory:
1. Introduction to Matlab/Octave
MATLAB (MATrix LABoratory) and Octave are two interactive software tools for numerical
computations and graphics. Both are especially designed for matrix computations: solving
systems of linear equations, performing matrix transformations, factoring matrices, and so
forth. In addition, they have a variety of graphical capabilities, and can be extended through
programs.

MATLAB and Octave are supported on Linux, Macintosh, and Windows environments.

Advantages of a scientific computing language:


• It simplifies the analysis of mathematical models.
• It frees you from coding in lower-level languages (saves a lot of time, with some
computational speed penalties)
• Provides an extensible programming/visualization environment, i.e., it provides
professional looking graphs
Scientific computing Language (MATLAB and Octave) Disadvantages:
All interpreted (i.e., not pre-compiled) languages are slower compared to C.

Getting Started

Using MATLAB/Octave for the first time is easy; mastering it can take years. In this
Laboratory session, we will introduce you to the MATLAB/Octave environment and show
you how to perform basic mathematical computations. After this lab, you should be able to
start using MATLAB for solving engineering problems.

No matter how you start it, once MATLAB/Octave opens, you should see the prompt ( >> ),
which tells you that Matlab/Octave is ready for you to enter a command. For example, the
MATLAB looks as in like Figure 1:

You can perform calculations in the command window in a manner similar to the way you
perform calculations on a scientific calculator.
For example, to compute the value of 5 squared, type the command at the command prompt:
>> 5^2
The following output will be displayed:
ans =
25

ENGR 3032, Term 443 2023 Page 2 of 8


Figure 1.1: The main GUI of Matlab.

2. Getting help
Three useful methods can be utilized to obtain help in Matlab and Octave:
Using the help command: The fastest way to find out information about commands is via the
help command. If you just type help and hit return, you will be presented with a menu of help
options. If you type ‘help’ followed by the name of a command/function,
MATLAB/Octave will provide help for that particular command. For example, ‘help hist’
will call up information on the ‘hist’ command, which produces histograms. Note that the
help command only displays a short summary of how to use the command you are interested
in.
Using the doc command: For more detailed help documentation, use the doc command. For
example, ‘doc hist’ will display the full documentation of the ‘hist’ command, including
examples of how to use it and sample output. The help system provides information to assist
you while using MATLAB/Octave.

Using the lookfor command: Another very useful MATLAB/Octave command is the
‘lookfor’ command. This will also search for commands related to a keyword. Find out which
MATLAB commands are available for plotting using the command ‘lookfor plot’ in the
command window.

Using the help menu item: select Help MATLAB Help to open the help pages. You can
then browse the commands via the Contents window; look through the Index of commands
or Search for keywords or phrases within the documentation.

ENGR 3032, Term 443 2023 Page 3 of 8


In Ocave, select Help Documentaiton and pick a topic to obtain more details about what
you are looking for.

Figure 1.2: Getting help in Matlab.

3. Saving Your Work – m script files


So far, all your work has been at the command line. This is useful for quick calculations. For
calculations that are more complex, or that you are likely to perform often it is much more
useful to save your work in script files. We will use script files from here onwards in the
course.

To create a new script file (also called an M-file) choose File New M-File from the
MATLAB menu bar (in Octave select New script icon or File → New script). This will open
a blank script editor window. You can enter commands into this window using the same
syntax as you would at the MATLAB/Octave command line. These commands can be saved,
repeated and edited.

Example 1: A room is 5 meters in height, 6 meters in width and 7 meters in length. What is
the volume of air that it can hold?

Solution:
Write a description of the solution (provide your name, ID number and a short description of
the problem).
define the corresponding variables:
height = 5
width = 4
find the unknown:

volume = height * width * length

ENGR 3032, Term 443 2023 Page 4 of 8


Figure 1.3: Complete solution to example 1 by using Octave (calculating volume).

Now save your script file by choosing File Save from the menu in the script file editor. Give
your script file a meaningful name, such as Example_1.m. The .m extension is used to denote
MATLAB/Octave script files. The file will be saved in the working directory.
Now go back to the MATLAB/Octave command line. First, clear the workspace using the
clear or clc commands. Now, at the command prompt enter Example_1 at the command
prompt to execute the script or simply select save and run icon that shows up when editing
the script.

The command line section should be like:


>> Example_1
height = 5
width = 6
length = 7
volume = 210
>>

ENGR 3032, Term 443 2023 Page 5 of 8


Task 1 : Using MATLAB Help to evaluate mathematical expression
Below is a table containing some expressions. To familiarize yourself with entering
expressions into MATLAB, complete the table. Use MATLAB/Octave Help to look up any
function description you don’t know,

e.g., >> lookfor log

or

>> help log

Remember that ln(x) is the natural logarithm and that the function ex is an exponential
function. Also, notice that the last expression produces a complex number, which
MATLAB/Octave represents using the special variable i for the imaginary part.

Mathematical MATLAB/ Octave Describe briefly the


Result
Expression Expression function / operation
1
1 1/sqrt(5*pi) 0.2523 Reciprocal of square root
√5𝜋
Arithmetic operations with
2 28.5 – 3.86/6.1+5.3 28.5 - 3.8^6/6.1 + 5.3 - 459.7961
power and division
3 7.6342 7.634^2 58.2780 raising to the power of 2
Trigonometric functions in
4 𝑐𝑜𝑠(5𝜋) + 𝑡𝑎𝑛(65𝑜 ) cos(5*pi) + tand(65) 1.1445
radians and degrees
5 e2 exp(2) 7.3891 Euler's number raised to 2
6 ln 3 log(3) 1.0986 Natural logarithm of 3
7 𝑙𝑜𝑔10 5 log10(5) 0.6990 Logarithm of 5 with base 10
8 5! factorial(5) 120 Factorial function of 5
Absolute value (magnitude)
9 | 4 - 4i | abs(4 - 4i) 5.6569
of a complex number
Note:
All angles are in radians by default (without a small circle).
A small circle indicates that the angle is in degrees.

ENGR 3032, Term 443 2023 Page 6 of 8


Task 2: Solve a problem using m-files

Follow the procedure in Example_1 in order to create a script file and save it in a folder with
file name Lab1_Task2_StudentID.m to calculate the following:

◼ Assume that r equals 4.618 cm and then find using MATLAB/Octave:

i. The area of a circle with radius r (Note: area is πr2).


ii. The surface area of a sphere with radius r (Note: surface of a sphere is 4πr2).
iii. The volume of a sphere with radius r (Note: the volume of a sphere is 4/3πr3).
iv. The volume of a cylinder with radius r for its base and height h=0.2 cm.

◼ Take a snapshot of the (Note: The snapshot should show your ID and folder name):
✓ your script (code)
✓ MATLAB/Octave output as shown in the Command window after executing the
script and paste it below:

Paste the script (m-file) here (screen shot) ...

Paste the results here (screen shot) ...

ENGR 3032, Term 443 2023 Page 7 of 8


Conclusion:

1. What are the general steps that you have followed to solve the problem given in task
2, using scientific computing languages?

Step 1: I assigned the values of the radius and height


Step 2: I wrote down the formulas for the area and volumes that we needed to
calculate
Step 3: I used MATLAB commands to perform the calculations by typing the
formulas in the script
Step 4: I ran the script to get the answers
Step 5: I printed the results to see the outputs

2. Name briefly the different methods to obtain help in Matlab / Octave ?

1: I can use the help command to find out what a function does
2: I can use the doc command to get more detailed information with examples
3: I can use lookfor to search for functions when I don’t know their exact names
4: I can also use the online documentation or built-in help browser in MATLAB

ENGR 3032, Term 443 2023 Page 8 of 8

You might also like