Docsity

MATLAB Programming Basics, Lecture notes of Matlab skills

Partial preview of the text

Download MATLAB Programming Basics and more Lecture notes Matlab skills in PDF only on Docsity!

MATLAB

Programming

Basics

Mapua University

CS10- Raymond B. Sedilla, MSIT

Programming

Languages

First high-level language: FORTRAN (1954)

To make programming easier especially to solve numerical problems

Today's languages: 1970s -

For commercial, "shrink-wrapped", large, complex programs: C, C++, Java

Algorithms

A correct algorithm solves the given computational problem

Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.

An algorithm is thus a sequence of computational steps that transform the input into the output.

Introduction to algorithms by Cormen et. al

Algorithms

A correct algorithm solves the given computational problem

Step 1: Input base Step 2: Input height Step 3: multiply 1/2, base and height Step 4: Output Area

Introduction to algorithms by Cormen et. al

Interpreters and Compilers

Two kinds of programs to process high-level language to low-level language

Interpreters - reads a high-level program and executes it. Compilers - reads the program and translates it completely before the program starts running. In this case the high-level program is the source code, and the translated program is called the object code, or the executable (JAVA and C++)

Interpreter

______________________________________________________________________________________

Source code

Interpreter Output "hello"

hello.m

More about programs

Program is a sequence of instructions that specifies how to

perform a computation. The computation might be something as

complex as showing an html page in a browser.

Input get the data from the keyboard, a file or some other device.

Output displays the data on the screen or send data to a file or other device

More about programs

Conditionals check for a certain conditions and execute the

appropriate sequence of statements.

Repetition performs some action repeatedly, usually with some variation

Formal vs Natural Languages

Natural languages are full of ambiguity, which people deal with by using contextual clues and other information. Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context.

Ambiguity

In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise.

Redundancy

Formal languages mean exactly what they say. On the other hand, natural languages are full of idiom and metaphor. If someone says, “The other shoe fell”, there is probably no shoe and nothing falling.

Literalness

A comment in a computer program is the text that is intended only for the human reader - It is completely ignored by the interpreter. In Matlab, the % token starts a comment. The rest of the line is ignored.

% Add up all the vector elements. y = sum(x) % Use the sum function.

MATLAB Comments

https://www.mathworks.com/help/matlab/matlab_prog/comments.html

A value is one of the fundamental things - like a word or a number - that program manipulates. Some values are 5. (The result when we add 2 + 3). These objects are classified. These objects are classified into different classes, or data types.

% Add two numbers num = 1 + 3

A data type represents a type of the data which you can process using your computer program. It can be numeric, alphanumeric, decimal, etc.

Values and Data types

Numeric Types - Integer and floating-point data Characters and Strings - Text in character arrays and string arrays Dates and Time - Arrays of date and time values that can be displayed in different formats Categorical Arrays - Arrays of qualitative data with values from a finite set of discrete, nonnumeric data Tables - Arrays in tabular form whose named columns can have different types Timetables - Time-stamped data in tabular form Structures - Arrays with named fields that can contain data of varying types and sizes Cell Arrays - Arrays that can contain data of varying types and sizes Function Handles - Variables that allow you to invoke a function indirectly

Data types in Matlab

https://www.mathworks.com/help/matlab/data-types.html

fprintf(class(12)); % will print double

fprintf(class("12.3")); % will print string

fprintf(class('Hello')); % will print characters

Data types in Matlab

https://www.mathworks.com/help/matlab/data-types.html

You can build complex expressions out of simpler ones using operators. Operators are special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands.

% Add two numbers num = (1 + 3) * 6

Remember that if we want to see the results of the computation, the program needs to specify that with the word fprintf.

Operators and Operands