MATLAB Introduction: Learning to Use MATLAB for Applied Mathematics and Sciences, Study notes of Computer Science

An introduction to using matlab for solving problems in applied mathematics and sciences. It covers how to start matlab, creating variables, and plotting functions. The document also includes sample scripts and examples for creating vectors, matrices, and performing matrix operations.

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-4jf-1
koofers-user-4jf-1 🇺🇸

9 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MA/CS 375
Spring 2009
Lecture 1a
Introduction to MATLAB: Some Basics
Ref: Appendix B, MATLAB Tutorial
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download MATLAB Introduction: Learning to Use MATLAB for Applied Mathematics and Sciences and more Study notes Computer Science in PDF only on Docsity!

MA/CS 375

Spring 2009

Lecture 1a Introduction to MATLAB: Some Basics Ref: Appendix B, MATLAB Tutorial

MATH/CS 375 Goals

  • To learn how to effectively use compu-

tational methods together with the state-

of-the-art software package, MATLAB,

to solve problems in applied mathe-

matics and the sciences, and

  • To learn something about the design

and analysis of these methods.

http://www.mathworks.com

Overview of MATLAB Windows

Matlab Variables

  • In Matlab variables are not declared by

the user but are created on a need-to-

use basis by a memory manager.

  • Moreover, from MATLAB’s point of

view, every simple variable is a complex

matrix indexed from unity.

  • Scalars are 1x1 matrices.

Script Ex

% Script(Ex1p): Plots the function f(x) = sin(2pix) % on the interval [0,1] using 11 equally-spaced points.

% Define x-points. n = 11; x = zeros(1,n); x = [0, .1, .2, .3, .4, .5, .6, .7,... .8, .9, 1];

% Evaluate y(x) = sin(2pix) for these x-points. y = zeros(1,n); for i = 1:n y(i) = sin(2pix(i)); end

% Plot sin(2pix) vs. x on [0,1]. plot(x,y);

EX1 Plot

EX1vp Plot/Print

% Script (Ex1vp): plots and prints the function f(x) = sin(2pix) % on the interval [0,1] using 11 equally-spaced points.

% Define x- and y-points. n = 11; x = linspace(0,1,n); y = zeros(1,n); y = sin(2pix);

% Print sin(2pix) vs. x on the interval [0,1]. disp(' x sin(2pix)') for i = 1:n disp(sprintf(' %3.1f %6.4f',x(i),y(i))); end

% Plot sin(2pix) vs. x on the interval [0,1]. plot(x,y);

Ex1vp output

>> ex1pvp x sin(2pix) 0.0 0. 0.1 0. 0.2 0. 0.3 0. 0.4 0. 0.5 0. 0.6 -0. 0.7 -0. 0.8 -0. 0.9 -0. 1.0 -0. >>

Building Scalars/Vectors

  • Row vector: x = [1, 2, 3];
  • Column vector: y = [1; 2; 3];
  • Transpose: [1, 2, 3]’ = [1; 2; 3]
  • Length of a vector: length(x)
  • linspace command: z = linspace(1,3,n)
    • Evaluate for n = 3
    • Evaluate for n = 5

Building a Matrix

Example of a matrix A

with 3 rows and 2

columns.

2.1 3. 4.12 1. 7.1 5

   ^       

A

>> A = zeros(3,2); >> A(1,1) = 2.1; >> A(1,2) = 3.23; >> A(2,1) = 4.12; >> A(2,2) = 1.893; >> A(3,1) = 7.1; >> A(3,2) = 5.; >> A A = 2.1000 3. 4.1200 1. 7.1000 5. >>

Now What?

In the next few Viewgraphs, we examine some standard operations with matrices that are allowed in MATLAB. +, -, , ., ^, .^, /, ./, , .\

Matrix Notation

2 3 2 22 23 2 3 32 33 3

2 3

A

M M M

N N N NM

11 1 1 1 1 1

1

A A A A

A A A A

A A A A

A A A A

Let A be an NxM matrix:

Matrix Addition in Matlab

2 1 3 2 4 2 4 1

               

A

B

C A B

>> A = [2, 1;3, 2];

>> B = [4, 2;4, 1];

>> C = A + B

C =

A Note on Matrix Addition

To add (or subtract) two matrices A and

B, the dimensions of A and B must be

the same:

of rows of A must equal # of rows of B

of columns of A must equal # of columns of B

>> A = [2, 1; 3, 2; 1, 4]; >> B = [4, 2; 4, 1]; >> C = A + B ??? Error using ==> plus Matrix dimensions must agree. >>