Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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 🇺🇸

10 documents

1 / 30

Toggle sidebar

Related documents


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 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. A Simple Problem: plotting a function Suppose we want to plot the function f(x) = sin(2 x) on the interval [0, 1]. To do this: – Set up a vector of x-values: 0 = x1 < x2 < … < xn = 1. – Evaluate the function at these x-values: yk = f(xk); k = 1, 2, …, n. – Display a plot connecting the points (x1 ,y1), … (xn, yn) by a polygonal line.  Script Ex1 % Script(Ex1p): Plots the function f(x) = sin(2*pi*x) % 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(2*pi*x) for these x-points. y = zeros(1,n); for i = 1:n y(i) = sin(2*pi*x(i)); end % Plot sin(2*pi*x) vs. x on [0,1]. plot(x,y); EX1vp Plot/Print % Script (Ex1vp): plots and prints the function f(x) = sin(2*pi*x) % 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(2*pi*x); % Print sin(2*pi*x) vs. x on the interval [0,1]. disp(' x sin(2*pi*x)') for i = 1:n disp(sprintf(' %3.1f %6.4f',x(i),y(i))); end % Plot sin(2*pi*x) vs. x on the interval [0,1]. plot(x,y); Ex1vp output >> ex1pvp x sin(2*pi*x) 0.0 0.0000 0.1 0.5878 0.2 0.9511 0.3 0.9511 0.4 0.5878 0.5 0.0000 0.6 -0.5878 0.7 -0.9511 0.8 -0.9511 0.9 -0.5878 1.0 -0.0000 >> Diary Command >> help diary diary Save text of MATLAB session. diary filename causes a copy of all subsequent command window input and most of the resulting command window output to be appended to the named file. If no file is specified, the file 'diary' is used. diary off suspends it. diary on turns it back on. diary, by itself, toggles the diary state. Use the functional form of diary, such as diary('file'), when the file name is stored in the string file. See also save. >> A More Direct Method 2.1 3.23 4.12 1.893 7.1 5           A >> A = [2.1, 3.23; 4.12, 1.893; 7.5, 5.] A = 1.2000 3.2300 4.1200 1.8930 7.5000 5.0000 >> A(3,1) =7.1 A = 2.1000 3.2300 4.1200 1.8930 7.1000 5.0000 >> 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: 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. >> Matrix Multiplication Matrix multiplication is defined by or For the (i,j) element of matrix C, we take the i’th row of A and multiply it, entrywise, by the j’th column of B 1 NcolsA ij ik kj k  C A B  C A B Multiplication Example 2 1 3 2 4 2 4 1                 A B C A B 2*4 1*4 2*2 1*1 3*4 2*4 3*2 2*1 12 5 20 8 C                 Entrywise Multiplication (.*) Example 1 2 , 3 4 5 6 7 8 A B               >> A = [1, 2; 3, 4]; >> B = [5, 6; 7, 8]; >> A.*B ans = 5 12 21 32 >> Example of Exponentiation, Traditional and Entrywise 1 2 3 4 A        >> A^2 ans = 7 10 15 22 >> A.^2 ans = 1 4 9 16 >> >> A = [1,2;3,4]; Functions in Matlab • Matlab has a number of built-in functions: – cos, sin, tan, acos, asin, atan – cosh, sinh, tanh, acosh, asinh, atanh – exp, log, sqrt • They all take matrices as arguments and return matrices of the same dimensions, e.g. cos([1 2 3]) • For other functions type: > help matlab\elfun
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved