0% found this document useful (0 votes)
3 views

Lab1 (1)

The document provides an overview of basic MATLAB programming concepts, including variable creation, arithmetic operators, elementary functions, conditional control structures, and loop control mechanisms. It includes examples for using 'if', 'for', and 'while' statements, as well as implementing the Fibonacci sequence. The document serves as a foundational guide for numerical analysis using MATLAB.

Uploaded by

bmessaad1970
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab1 (1)

The document provides an overview of basic MATLAB programming concepts, including variable creation, arithmetic operators, elementary functions, conditional control structures, and loop control mechanisms. It includes examples for using 'if', 'for', and 'while' statements, as well as implementing the Fibonacci sequence. The document serves as a foundational guide for numerical analysis using MATLAB.

Uploaded by

bmessaad1970
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Numerical Analysis Lab Note #1

Matlab Basic

===================================================================
Variables

Like most other programming languages, the MATLAB language provides


mathematical expressions, but MATLAB does not require any type
declarations or dimension statements. When MATLAB encounters a new
variable name, it automatically creates the variable and allocates
the appropriate amount of storage. If the variable already exists,
MATLAB changes its contents and, if necessary, allocates new
storage. For example,

>>num_students = 9

>>lab_num = 11

>>quiz_num = lab_num

creates a variable named "num_students" and stores the value 9 in


its single element. To view the variable, simply enter the variable
name.

===================================================================
Operators

Expressions use familiar arithmetic operators and precedence rules.

+ Addition

- Subtraction

* Multiplication

/ Division

^ Power

Example:

1
>> % Evaluate f(x)=x^3+2*x^2+x/2-2 @ x=1.5;
>> x = 1.5
>> x^3+2*x^2+x/2-2

===================================================================
Elementary Functions

MATLAB provides a large number of standard elementary mathematical


functions, including "abs", "sqrt", "exp", and "sin". Taking the
square root or logarithm of a negative number is not an error; the
appropriate complex result is produced automatically. MATLAB also
provides many more advanced mathematical functions, including Bessel
and gamma functions. Most of these functions accept complex
arguments. For a list of the elementary mathematical functions, type

>>help elfun

Example:

>> sqrt(3)+abs(-4)+exp(2)

===================================================================
Conditional Control: if, else

The "if" statement evaluates a logical expression and executes a


group of statements when the expression is true. The optional
"elseif" and "else" keywords provide for the execution of alternate
groups of statements. An end keyword, which matches the if,
terminates the last group of statements. The groups of statements
are delineated by the four keywordsno braces or brackets are
involved. Example

>>A=5; B=3;
>>if A > B
’greater’
elseif A < B
’less’
elseif A == B
’equal’
else
error(’Unexpected situation’)
end
>>

===================================================================
Loop Control: for, while

2
The "for" loop repeats a group of statements a fixed, predetermined
number of times. A matching "end" delineates the statements:

>>% compute 1+2+...+100 using for


>>total=0;
>>for n = 1:100
total = total + n;
end
>>total

The "while" loop repeats a group of statements an indefinite number


of times under control of a logical condition. A matching "end"
delineates the statements.

>> % compute 1+2+...+100 using while


>> total = 0; n=1;
>> while n<= 100
total = total +n;
n = n+1;
end
>> total

===================================================================
Example:
Implement the Fibonacii sequence: F(n)=F(n-1)+F(n-2),
F(1)=F(2)=1; Show F(n) for n=1,2,..., 20

>> % Using for loop


F1=1;
F2=1;

for n = 3 : 20
F=F1+F2;
disp([’n=’ num2str(n) ’; F(’ num2str(n) ’)=’ num2str(F)]);
F1=F2;
F2=F;
end

>> % Using while loop


F1=1;
F2=1;

disp([’n=’ num2str(1) ’; F(’ num2str(1) ’)=’ num2str(F1)]);


disp([’n=’ num2str(2) ’; F(’ num2str(2) ’)=’ num2str(F2)]);

3
n=3;
while n <= 20
F=F1+F2;
disp([’n=’ num2str(n) ’; F(’ num2str(n) ’)=’ num2str(F)]);
F1=F2;
F2=F;
n=n+1;
end

You might also like