matlab tutorial

8
  More on polynomials. >> x=-4.0:0.25:6.0; >> p=[2, 3, 4, -5, -6, 3]; % 2x^5 + 3x^4 + 4x^3 -5x^2 -6x +3 >> v=polyval(p,x); >> roots(p) ans = -0.8954 + 1.6642i -0.8954 - 1.6642i -1.0654 0.9342 0.4220 plot(x,v,'LineWidth',3,'color','r') >> -4 -3 -2 -1 0 1 2 3 4 5 6 -0.5 0 0.5 1 1.5 2 2.5 x 10 4  Regression curve and polynomial fit: >> n=2; >> q=polyfit(x,v,n) % quadratic curve fitting

Upload: tanzid-sultan

Post on 05-Oct-2015

214 views

Category:

Documents


0 download

DESCRIPTION

Matlab Tutorial

TRANSCRIPT

  • More on polynomials. >> x=-4.0:0.25:6.0; >> p=[2, 3, 4, -5, -6, 3]; % 2x^5 + 3x^4 + 4x^3 -5x^2 -6x +3 >> v=polyval(p,x); >> roots(p) ans = -0.8954 + 1.6642i -0.8954 - 1.6642i -1.0654 0.9342 0.4220 plot(x,v,'LineWidth',3,'color','r') >>

    -4 -3 -2 -1 0 1 2 3 4 5 6-0.5

    0

    0.5

    1

    1.5

    2

    2.5x 10

    4

    Regression curve and polynomial fit: >> n=2; >> q=polyfit(x,v,n) % quadratic curve fitting

  • q = 1.0e+003 * 0.3369 0.4999 -1.6000 >> n=1; >> q=polyfit(x,v,n) % linear line fitting q = 1.0e+003 * 1.1738 1.0111 >> R=corrcoef(x,v); % obtains correlation coefficient R >> R(1,2) ans = 0.7214 >> >> clear >> sales = round(100*rand(30,4)) sales = 81 71 75 8 91 3 26 5 13 28 51 53 91 5 70 78 63 10 89 93 10 82 96 13 28 69 55 57

  • 55 32 14 47 96 95 15 1 96 3 26 34 16 44 84 16 97 38 25 79 96 77 81 31 49 80 24 53 80 19 93 17 14 49 35 60 42 45 20 26 92 65 25 65 79 71 62 69 96 75 47 75 66 28 35 45 4 68 83 8 85 66 59 23 93 16 55 91 68 12 92 15 76 50 29 83 74 96 76 54 39 34 75 100 66 59 38 8 17 22 57 44 >> sorted_sales=sort(sales) sorted_sales = 4 3 14 1 10 3 15 5 13 5 20 8 14 10 24 8 16 12 25 8 17 16 25 13 28 19 26 15

  • 39 22 26 16 42 28 29 17 49 28 35 23 55 32 35 26 63 34 38 31 66 38 47 34 66 44 51 44 68 45 55 45 74 49 55 47 76 50 57 53 79 59 59 53 80 65 62 54 81 66 70 57 85 68 75 60 91 69 75 65 91 71 76 69 92 71 81 75 93 75 83 78 96 77 84 79 96 80 89 83 96 82 92 91 96 95 93 93 97 96 96 100 >> days=1:30; >> subplot(2,2,1); >> plot(days, sales(:,1)) >> subplot(2,2,2); >> plot(days, sales(:,2)) >> subplot(2,2,3); >> plot(days, sales(:,3)) >> subplot(2,2,4); >> plot(days, sales(:,4)) >>

  • 0 10 20 300

    20

    40

    60

    80

    100

    0 10 20 300

    20

    40

    60

    80

    100

    0 10 20 300

    20

    40

    60

    80

    100

    0 10 20 300

    20

    40

    60

    80

    100

    Also, >> R=corrcoef(sales) R = 1.0000 -0.0636 -0.2455 0.1253 -0.0636 1.0000 -0.0070 -0.1968 -0.2455 -0.0070 1.0000 -0.0851 0.1253 -0.1968 -0.0851 1.0000 >> R=corrcoef(sorted_sales) R = 1.0000 0.9634 0.9512 0.9464

  • 0.9634 1.0000 0.9891 0.9880 0.9512 0.9891 1.0000 0.9936 0.9464 0.9880 0.9936 1.0000 >> The sorted sales profiles of city 3 and city 4 are highly correlated. >> % Some statistical functions. >> x=rand(1,80); % A row of 80 random number between 0 and 1.0 >> x=100*rand(1,80); % A row of 80 random number between 0 and 100.0 >> avg = mean(x) avg = 49.5715 >> sigma_x = std(x) sigma_x = 27.2316 >> median_x = median(x) median_x = 47.9766 >> variance= var(x) variance =

  • 741.5610 >> sqrt(variance) ans = 27.2316 >> >> clear >> clear >> x=linspace(-1.0,0.2,50); >> y=humps(x); >> plot(x,y) >> x=linspace(-1.0,2.5,50); >> y=humps(x); >> plot(x,y)

    -1 -0.5 0 0.5 1 1.5 2 2.5-20

    0

    20

    40

    60

    80

    100

    >> % Integrate function from -1.0 to 2.5 >> area=trapz(x,y); % Use trapezeum rule to evaluate the integral >> area area =

  • 23.7579 >>