Introduction to Scientific Computing - - A Matrix Vector Approach Using Matlab Written by Charles F.Van Loan 陈 文 斌 // ORG: 复旦大学 (Revised.

Slides:



Advertisements
Similar presentations
Problems and solutions Session 3. Introduction to MATLAB - Solutions 3 Problems 1. Write function Xn = mspolygon(X,x0,a) that scales the INPUT polygon.
Advertisements

Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Introduction to Matlab
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
Matlab Graphics S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: 2D Graphics.
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
MATLAB - Basics Centro de Informática Universidade Federal de Pernambuco Aprendizagem de Máquina – IN1102 Arley Ristar –
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Math.375 Fall Errors in Numerical Computation Vageli Coutsias.
Welcome to EGR 106 Foundations of Engineering II Course information Today’s specific topics: – Computation and algorithms – M ATLAB Basics Demonstrations.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Getting started with Matlab Numerical Methods Appendix B help/techdoc/learn_matlab/learn_matlab.html.
Matlab intro The Environment
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB Fundamentals.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 4 Curve Fitting.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Yasser F. O. Mohammad Approximations and Round-off Errors.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
INTRODUCTION TO MATLAB LAB# 01
Recap Sum and Product Functions Matrix Size Function Variance and Standard Deviation Random Numbers Complex Numbers.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
MATLAB Tutorial EE313 Signals and Systems Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Jeff Andrews.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots.
Introduction to Matlab
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 2 in MATLAB Topics Covered: 1.More about 2-D plots  Placing multiple.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Intro to Matlab Rogelio Long September 3, How to access MyDesktop Log in with your utep id and password.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
PATTERN RECOGNITION LAB 2 TA : Nouf Al-Harbi::
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to Mat lab
Chapter 3 Arrays and Vectors
Computer programming Dr. Ivan A. Hashim.
Matrices and Arrays.
Seminar 1 for DCSP Using Matlab.
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
MatLab – 2D Plots 2 MATLAB has many built-in functions and commands to create various types of plots. Instructor notes: We start with an example of some.
Math.375 Fall 2005 Errors in Numerical Computation
Announcements P3 due today
Math.375 Fall 2005 I - Numbers and Formats
-seminar 1 for digital signal processing
Computer Simulation Lab
Math 375 Fall Functions Vageli Coutsias.
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Introduction to Scientific Computing - - A Matrix Vector Approach Using Matlab Written by Charles F.Van Loan 陈 文 斌 // ORG: 复旦大学 (Revised :NTUST.ME.2007Q4) 展示 PLOT( ) 的主要機能

Chapter 1-Power tools of the trade 1.Vectors and Plotting 2.More Vectors, More Plotting and New Matrices 3.Building Exploratory Environments 4.Error 5.Designing Functions

Six windows

intro

demo

Vectors and Plotting Setting Up Vectors

X=[ ] X=[10.1; 20.2; 30.3] X=[10.1; 20.2; 30.3]; X=[ ]' Q – Find the differences in above lines

X=[ … ] n=21; h=1/(n-1); for k=1:n x(k)=(k-1)*h; end n=21; h=1/(n-1); x=zeros(1,n); for k=1:n x(k)=(k-1)*h; end 1 "Dimension mismatch" error 2. Efficient(memory)

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 variable is a complex matrix indexed from unity.

length u=[ ]; n=length(u); v=[10;20;30;40]; m=length(v); u=[50 60]; p=length(u); help length LENGTH Length of vector. LENGTH(X) returns the length of vector X. It is equivalent to MAX(SIZE(X)) for non-empty arrays and 0 for empty ones. help length LENGTH Length of vector. LENGTH(X) returns the length of vector X. It is equivalent to MAX(SIZE(X)) for non-empty arrays and 0 for empty ones.

help help who help whos help clear help for help zeros help ; help [] help

Regular vectors x=20: 24 x=20: 2: 29; x=10: -1 : 1 x=3: 2 colon notation : :

linspace x=linspace(a,b,n) x k =a+(k-1)*(b-a)/(n-1) x=linspace(0,1,10) Linspace(Left Endpoint, Right Endpoint, Number of Points)

logspace x=logspace(-2,2,6) x=logspace(a,b,n) m=linspace(a,b,n) for k=1:n x(k)=10^m(k) ; end linspace(a,b) linspace(a,b,100) logspace(a,b) logspace(a,b,50) linspace(a,b) linspace(a,b,100) logspace(a,b) logspace(a,b,50)

format x= *logspace(1,5,5)' Format short Format long Format short e Format long e x?

Evaluating Functions n=21; x=linspace(0,1,n); y=zeros(1,n); for k=1:n y(k)=sin(x(k)); end n=21; x=linspace(0,1,n); y=sin(x); vectorization help elfun

Vectorization Speed Clarity Education

Example m=5; n=4*m+1; x=linspace(0,1,n); y=zeros(1,n); a=x(1:m+1); y(1:m+1) = sin(2*pi*a); y(2*m+1:-1:m+2)= y(1:m); y(2*m+2:n)=-y(2:2*m+1); y=sin(2*pi*linspace(0,1,21)) ??

% SineTable, Prints a short table of sine evaluations. clc n = 21; x = linspace(0,1,n); y = sin(2 * pi * x); disp(' ') disp(' k x(k) sin(x(k))') disp(' ') for k=1:21 degrees = (k-1)*360/(n-1); disp(sprintf(' %2.0f %3.0f %6.3f ',k,degrees,y(k))); end disp( ' '); disp('x(k) is given in degrees.') disp(sprintf('One Degree = %5.3e Radians',pi/180)) Displaying Tables--sprintf Sinetable.m Help sinetable

A Simple Plot n=20; %n=200; x=linspace(0,1,n); y=sin(2*pi*x); hold on plot(x,y) %plot(x,y,'r') title('The function y=sin(2*pi*x)') xlabel('x (in radians)') ylabel('y') The function y=sin(2*pi*x) x (in radians) y

The function y=sin(2*pi*x) x (in radians) y n=??

% Script File: SinePlot % Displays increasingly smooth plots of sin(2*pi*x). close all pause(2) for n = [ ] x = linspace(0,1,n); y = sin(2*pi*x); plot(x, y) title(sprintf('Plot of sin(2*pi*x) based … upon n = %3.0f points.',n)) pause(2) end

Vectorizing Function Evaluations n=200; x=linspace(0,1,n); y=zeros(1,n); for k=1:n y(k)=((1-x(k)/24)/(1+x(k)/24+(x(k)/384)*x(k)))^8 end plot(x,y)

% Script File: ExpPlot % Examines the function % f(x) = ((1 - x/24)/(1 + x/24 + x^2/384))^8 % as an approximation to exp(-x) across [0,1]. x=linspace(0,1,200); y=((1-x/24)./(1+x/24+(x/384).*x)).^8 plot(x,y) title('(1-x/24)/(1+x/24+x^2/384))^8').^ pointwise vector exponentiation./ pointwise vector divide.* pointwise vector multiple PointWise Operation

Scaling and superpositioning x=linspace(-pi/2,11*pi/2,200); y=tan(x); plot(x,y) axis([-pi/2 9*pi/ ]) autoscaling Axis([xmin xmax ymin ymax])

% Plots the function tan(x), -pi/2 <= x <= 9pi/2 close all; ymax = 10; x = linspace(-pi/2,pi/2,40); y = tan(x); plot(x,y) axis([-pi/2 9*pi/2 -ymax ymax]) title('The Tangent Function');xlabel('x'); ylabel('tan(x)') hold on for k=1:4 xnew = x+ k*pi; plot(xnew,y); End hold off The Tangent Function x tan(x)

x=linspace(0,1,200); y1=sin(2*pi*x); y2=cos(2*pi*x); plot(x,y1); hold on; plot(x,y2,'--'); plot([1/8 5/8],[1/sqrt(2) -1/sqrt(2)],'*') hold off x=linspace(0,1,200); y1=sin(2*pi*x); y2=cos(2*pi*x); plot(x,y1,x,y2,'--',[1/8 5/8],[1/sqrt(2) -1/sqrt(2)],'*') Plot(….)

% Plots selected regular polygons. close all ; clc theta = linspace(0,2*pi,361); c = cos(theta); s = sin(theta); k=0; for sides = [ ] stride = 360/sides; k=k+1; subplot(3,3,k) plot(c(1:stride:361),s(1:stride:361)) axis([ ]) axis equal end interesting Subplot(m,n,k):Split the current figure into an m-row by n column array of subwindows that are indexed left to right,top to bottom

More Fun… Optional Slides (After this One) –Linspace ( ) vs. logspace ( ) –Random Numbers :-- rand() vs. randn( ) –Chart : – hist( ) –Plot( ) vs. Subplot ( ) –Hold on vs. Hold off –Printf ( ) vs. sprintf ( )

Building Exploratory Environments

Random Process

close all subplot(2,1,1); x = rand(1000,1); hist(x,30); axis([ ]) title('Distribution of Values in rand(1000,1)') xlabel(sprintf('Mean = %5.3f. Median = … %5.3f.',mean(x),median(x))) subplot(2,1,2); x = randn(1000,1); hist(x,linspace(-2.9,2.9,100)); title('Distribution of Values in randn(1000,1)') xlabel(sprintf('Mean = %5.3f. Standard Deviation = … %5.3f',mean(x),std(x)))

Clouds.m

close all; Points = rand(1000,2); subplot(1,2,1) plot(Points(:,1),Points(:,2),'.') title('Uniform Distribution.') axis([ ]) axis square Points = randn(1000,2); subplot(1,2,2) plot(Points(:,1),Points(:,2),'.') title('Normal Distribution.') axis([ ]) axis square

Outcome of 1000 Dice Rolls.

% Script File: Dice % Simulates 1000 rollings of a pair of dice. close all First = 1 + floor(6*rand(1000,1)); Second = 1 + floor(6*rand(1000,1)); Throws = First + Second; hist(Throws, linspace(2,12,11)); title('Outcome of 1000 Dice Rolls.') First = ceil(6*rand(1000,1));

 's estimate by Monte Carlo Hundreds of Trials Q: Circle – How to draw this ?

Error Focus on the mathematical errors that arise through discretization and the rounding errors that arise due to finite precision arithmetic

Absolute and Relative Error Absolute error Relative error If the relative error is 10 -d, then has approximately d correct significant digits

% Script File: Stirling % Prints a table showing error in Stirling's formula for n! disp(' Stirling Absolute Relative') disp(' n n! Approximation Error Error') disp(' ') e=exp(1);nfact=1; for n=1:13 nfact = n*nfact; s = sqrt(2*pi*n)*((n/e)^n); abserror = abs(nfact - s); relerror = abserror / nfact; s1 = sprintf(' %2.0f %10.0f %13.2f',n,nfact,s); s2 = sprintf(' %13.2f %5.2e',abserror,relerror); disp([s1 s2]) end Stirling

Stirling Absolute Relative n n! Approximation Error Error e e e e e e e e e e e e e-003

% Script File: ExpTaylor Plots, as a function of n, the relative error in %the Taylor approximation 1 + x + x^2/2! x^n/n! to exp(x). nTerms = 50; for x=[ ] figure; f = exp(x)*ones(nTerms,1); s = 1; term = 1; for k=1:50 term = x.*term/k; s = s+ term; err(k) = abs(f(k) - s); end relerr = err/exp(x); semilogy(1:nTerms,relerr) ylabel('Relative Error in Partial Sum.'); xlabel('Order of Partial Sum.') title(sprintf('x = %5.2f',x)) end

??bottom out x=10 Relative error x=-10 x=5 x=1

Rounding Errors The plots reveal that the mathematical convergence theory does not quit apply The errors do not go to zero as the number of terms in the series increases. The relative error for x=-10 is much worse that for x=10 Floating point arithmetic! mathematicsDevelopment of algorithms Inexact computer arithmetic system

End Homework for NTUST-ME.2007Q4 –Please write down here….