Your SlideShare is downloading. ×
Numerical computing with matlab
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Numerical computing with matlab

496
views

Published on

numerical computing basics with matlab

numerical computing basics with matlab

Published in: Education, Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
496
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
44
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Numerical Computing with Matlab by M. Jamil Farooq
  • 2. Road Map
    • Basic Matlab Skills
    • 3. Solving Non Linear Equations
    • 4. Sets of Linear Equations
    • 5. Interpolations
    • 6. Numerical Differentiation
    • 7. Numerical Integration
  • 8. Introduction to Matlab
  • 9. Features:
    • MATLAB is a high-level technical computing language.
    • 10. For Algorithm Development.
    • 11. Data Visualization.
    • 12. Data Analysis.
    • 13. Numeric Computations.
  • 14. Features:
    • Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration.
    • 15. 2-D and 3-D graphics functions for visualizing data.
    • 16. Tools for building custom graphical user interfaces.
    • 17. Goto http://www.mathworks.com/help/pdf_doc/allpdf.html
  • 18. Environment:
  • 19. Topics:
    • Matrices and Arrays
      • Representation
      • 20. Basic Operations
      • 21. Array operations
    • Graphics
      • Plotting 2D and 3D data
    • Programming in Matlab
      • Writing scripts
      • 22. Writing functions
  • 23. Matrices: A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] MATLAB displays the matrix you just entered: 16 3 2 13 A = 5 10 11 8 9 6 7 12 4 15 14 1 See the results of sum(A) gives sum of columns A' gives transpose of A sum(A')' gives transpose of sum of transpose of A diag(A) diagonal of A sum(diag(A)) sum of diagonal of A fliplr(A) flip A from left to right sum(fliplr(A)) sum of A by flipping A from left to right
  • 24. More about Matrices:
    • Subscript to access individual elements
            • A ( i , j ) : access i th row and j th column e.g A ( 1 , 2 )
    • Access with single subscript
            • A ( 2 ) is A ( 2, 1 ) and A ( 10 ) is A ( 2 , 3 )
      • Index can't exceed matrix dimension when right side of expression
            • e.g. T = A ( 4 , 5 ) : illegal expression
      • Index can exceed matrix dimension when on the left side
            • e.g. X = A
            • 25. X ( 4 , 5 ) = 5 : legal expression
      • Colon Operator:
            • 1 : 10
            • 26. 1 : 1 : 10 100 : -7 : 50
            • 27. 1 : 2 : 10 1 : 0.2 : 10
            • 28. 0: pi/4 : pi
      • Colon as matrix subscript:
            • A ( : , 1 ) is all rows of 1 st column.
            • 29. A (1 : 3 , 1 ) is 1 st to 3 rd row of 1 st column
            • 30. sum( A (2 : end , 1 ) ) is 2 nd to last row of 1 st column
  • 31. Expressions:
    • rho = ( 1 + sqrt ( 5 ) ) / 2
    • 32. theta = 0 : pi/4 : pi; b= sin(Theta);
    • 33. a = abs ( 3 + 4 i )
    • 34. b = exp( 3 + 4 i )
    • 35. Z = zeros(2,4)
                      • Z = 0 0 0 0
    • 0 0 0 0
    • 36. F = 5*ones(3,3)
                      • F = 5 5 5
    • 5 5 5
    • 37. 5 5 5
    • 38. N = fix(10*rand(1,10))
                      • N = 9 2 6 4 8 7 4 0 8 4
  • 39. Expressions:
    • R = randn(4,4): normal random numbers
    • 40. R = 0.6353 0.0860 -0.3210 -1.2316
    • 41. -0.6014 -2.0046 1.2366 1.0556
    • 42. 0.5512 -0.4931 -0.6313 -0.1132
    • 43. -1.0998 0.4620 -2.3252 0.3792
    • 44. Concatenation:
                  • B= [A A + 32 ; A + 48 A + 16]
      • Deleting Rows and Columns:
                  • X=A
                  • 45. X( : , 1 ) =[ ] delete first colum of X
                  • 46. X( 2 , : ) =[ ] delete second row of X
  • 47. Expressions:
    • R = randn(4,4): normal random numbers
    • 48. R = 0.6353 0.0860 -0.3210 -1.2316
    • 49. -0.6014 -2.0046 1.2366 1.0556
    • 50. 0.5512 -0.4931 -0.6313 -0.1132
    • 51. -1.0998 0.4620 -2.3252 0.3792
    • 52. Concatenation:
                  • B= [A A + 32 ; A + 48 A + 16]
      • Deleting Rows and Columns:
                  • X=A
                  • 53. X( : , 1 ) =[ ] delete first colum of X
                  • 54. X( 2 , : ) =[ ] delete second row of X
  • 55. Linear Algebra:
      • A = magic( 4 ) generate magic square
      • 56. A + A' can add addable matrices
      • 57. A' * A multiply compatible matrices for multiplication
      • 58. det( A ) determinant of A
      • 59. R = rref ( A ) row reduce echelon form
  • 60. Symbol processing: Create symbols: syms x; Create functions: f = 2*x^2 - 3*x + 1; Evaluate functions subs(f, 2) ans = 3
  • 61. Linear Algebra:
      • A = magic( 4 ) generate magic square
      • 62. A + A' can add addable matrices
      • 63. A' * A multiply compatible matrices for multiplication
      • 64. det( A ) determinant of A
      • 65. R = rref ( A ) row reduce echelon form
  • 66. Flow control
    • If statement:
    • 67. % Generate a random number
    • 68. a = randi(100, 1);
    • 69. % If it is even, divide by 2
    • 70. if rem(a, 2) == 0
      • disp('a is even')
      • 71. b = a/2;
    • end
    • If elseif and else
    • 72. a = randi(100, 1);
    • 73. if a < 30
      • disp('small')
    • elseif a < 80
      • disp('medium')
    • else
      • disp('large')
    • end
  • 74. Flow control
    • Switch statement:
    • 75. [dayNum, dayString] = weekday(date, 'long');
    • 76. switch dayString
    • 77. case 'Monday'
    • 78. disp('Start of the work week')
    • 79. case 'Tuesday'
    • 80. disp('Day 2')
    • 81. case 'Wednesday'
      disp('Day 3') case 'Thursday' disp('Day 4') case 'Friday' disp('Last day of the work week') otherwise disp('Weekend!') end
  • 82. Flow control
    • While statements:
    • 87. a = 0; fa = -Inf; b = 3; fb = Inf;
    • 88. while b-a > eps*b
      • x = (a+b)/2;
      • 89. fx = x^3-2*x-5;
      • 90. if sign(fx) == sign(fa)
        • a = x; fa = fx;
      • else
        • b = x; fb = fx;
      • end
    • end
    • 91. x
  • 92. Functions
      function f = fibonacci(n) %FIBONACCI sequence % the first n Fibonacci numbers. f = zeros(n,1); f(1) = 1; f(2) = 2; for k = 3:n f(k) = f(k-1) + f(k-2); end
  • 99. Solution of Nonlinear equations
    • Problem Statement:
      • Finding zero/root of the non-linear function/equation f(x) in the given interval [a, b].
      • 100. Stopping criteria:
        • Fixed number of iterations for any given method.
        • 101. Absolute of relative error for successive iterations must yield pre specified tolerance TOL.
    • Methods
  • 105. Interval Halving(Bisection)
      While( relative error > Tolerance )
        x=( a + b ) / 2 //mid value of a and b if ( f(x) * f(a) > 0) //i.e f(x) and f(a) has same sign
          a=x;
        else
          b=x;
        end if
      End while
  • 106. Secant Method
    • Assume f(x) to be linear near root.
    • 107. Choose two points near roots as initial guess [x0 x1].
    • 108. Interpolate/extrapolate the third point using linear interpolation.
    • 109. Using similar triangle
      • (x1-x2)/f(x1) = (x0-x1)/(f(x0)-f(x1))
    • Reformulate:
      • x2=x1-f(x1)(x0-x1)/(f(x0)-f(x1))
  • 110. Secant Method
      if(|f(x0)|<|f(x1)|)
        swap x0,x1
      Repeat
        Cal x2 with formula x0=x1; x1=x2;
      Until |f(x2)| < TOL
  • 111. False Position Method
    • Root approximated always within the interval [x0, x1] similar to bisection method.
    • 112. Root approximation is calculated using the same formula as Secant method.
    • 113. Performance is better than bisection.
      Repeat
        calculate x2 using secant formula if (f(x0)*f(x2)>0)
          x0=x2
        else
          x1=x2
        Endif
      until |f(x2)|<TOL
  • 114. Newton's Method
    • Uses x0 as initial guess.
    • 115. Draw tangent at successive approximation to find the next approximation of root.
  • 116. Newton Algorithm
      Compute f(x0) and f`(x0) While (f(x0)~=0 and f`(x0) ~=0)
      • Set x1=x0
      • 117. Set x0=x0-f(x0)/f`(x0)
      • 118. if(|x0-x1|<TOL or |f(x0)|<TOL) break;
      End while
      Symbolic Math Functions sym x; f=sym('x^3-2*x^2-x+5'); subs(f,2) solve(f) df=diff(f) eval(solve(f))
  • 119. Problem Solving
    • Assume v is 1000 euros and that after 5 years M is equal to 6000 euros.
    • 120. Plot the function.
    • 121. Find out the root using bisection method.
  • 122.  
  • 123.  
  • 124.
      Given the function f (x) = cosh x + cos x − γ,
    • for γ = 1, 2, 3 find an interval that contains the zero of f . Then compute the zero by the bisection method with a tolerance of 10^−10 .
    • 125. Apply Newton’s method to solve. Why is this method not accurate when γ = 2?
  • 126. Interpolation
  • 127. Divided Difference Table