Home >Documents >Introducere in Matlab

Introducere in Matlab

Date post:22-Oct-2015
Category:
View:75 times
Download:2 times
Share this document with a friend
Description:
Introducere Matlab
Transcript:
  • Introducere in Matlab

  • Continut:

    Ce este Matlab? Ecranul Matlab Variabile, vectori, matrice, indecsi Operatori (Aritmetic, relational, logic ) Facilitati afisare Controlul fluxului Utilizarea fisierelor M Functii definite de utilizator Concluzii

  • Ce este Matlab?

    Matlab este un limbaj de nivel inalt ce are numeroase colectii de unelte pentru a facilita lucrul utilizatorului

    Cat de inalt?

    Assembly

    High Level Languages such as

    C, Pascal etc.

    Matlab

  • Ce ne intereseaza?

    Matlab este o unealta ampla, dar la acest curs il vom folosi numai in mica parte.

    Functionalitatile ce ne sunt necesare sunt:Matlab

    CommandLine

    m-files

    functions

    mat-files

    Command execution like DOS command

    window

    Series of Matlab

    commands

    InputOutput

    capability

    Data storage/ loading

  • Ecranul Matlab

    Command Window type commands

    Current Directory View folders and m-files

    Workspace View program variables Double click on a variable

    to see it in the Array Editor

    Command History view past commands save a whole session

    using diary

  • Variabile

    Nu necesita declaratii explicite de tip. i.e.,

    Toate variabilele sunt create implicit in dubla precizie si sunt matrice.

    Am declarat 2 variabile matrice 1x1 cu dubla precizie.

    int a;double b;float c;

    Example:>>x=5;>>x1=2;

  • Vectori, Matrice

    un vector x = [1 2 5 1]

    x =

    1 2 5 1

    o matrice x = [1 2 3; 5 1 4; 3 2 -1]

    x =

    1 2 35 1 43 2 -1

    transpusa y = x y =1251

  • Sir, Matrice

    t =1:10

    t =1 2 3 4 5 6 7 8 9 10

    k =2:-0.5:-1

    k =2 1.5 1 0.5 0 -0.5 -1

    B = [1:4; 5:8]

    x =

    1 2 3 45 6 7 8

  • Generarea vectorilor cu ajutorul functiilor

    zeros(M,N) MxN matrix of zeros

    ones(M,N) MxN matrix of ones

    rand(M,N) MxN matrix of uniformly distributed random numbers on (0,1)

    x = zeros(1,3)x =

    0 0 0

    x = ones(1,3)x =

    1 1 1

    x = rand(1,3)x =

    0.9501 0.2311 0.6068

  • Indexarea matricelor

    The matrix indices begin from 1 (not 0 (as in C)) The matrix indices must be positive integer

    Given:

    A(-2), A(0)

    Error: ??? Subscript indices must either be real positive integers or logicals.

    A(4,2)Error: ??? Index exceeds matrix dimensions.

  • Concatenarea Matricelor

    x = [1 2], y = [4 5], z=[ 0 0]

    A = [ x y]

    1 2 4 5

    B = [x ; y]

    1 24 5

    C = [x y ;z] Error:??? Error using ==> vertcat CAT arguments dimensions are not consistent.

  • Operatori (aritmetici)

    + addition- subtraction* multiplication/ division^ power complex conjugate transpose

  • Operatii cu matrice

    Given A and B:

    Addition Subtraction Product Transpose

  • Operatori (Element cu Element)

    .* element-by-element multiplication

    ./ element-by-element division

    .^ element-by-element power

  • Utilizarea . Element Operatie

    K= x^2Erorr:??? Error using ==> mpower Matrix must be square.

    B=x*yErorr:??? Error using ==> mtimes Inner matrix dimensions must agree.

    A = [1 2 3; 5 1 4; 3 2 1]A =

    1 2 35 1 43 2 -1

    y = A(3 ,:)

    y= 3 4 -1

    b = x .* y

    b=3 8 -3

    c = x . / y

    c= 0.33 0.5 -3

    d = x .^2

    d= 1 4 9

    x = A(1,:)

    x=1 2 3

  • Functii predefinite in Matlab

    Functie: nume, argumente (intrari), valori intoarse(iesiri) In general argumentele pot fi atat scalari cat si matriceExemplu x = 9;b = sqrt(x) intoarceb =

    3x = [4, 9, 16];b = sqrt(x) intoarceb =

    2 3 4

  • Functii predefinite in Matlab

    Unele functii pot avea mai multi parametri:Ex. Restul = rem(10,3) restul impartirii intregiRestul = 1Sau pot intoarce mai multe valorid = [1, 2, 3; 4, 5, 6];f = size(d) numarul de linii si coloane din matricef = 2 3

  • HELP in Matlab

    Functia in linie de comanda help Documentatia cuprinzatoare in format HTML Documentatia online Exemplu:help tanTAN Tangent of argument in radians.TAN(X) is the tangent of the elements of X.See also atan, tand, atan2.doc tan

  • Functii elementare matematice

    Abs(x) valoarea absoluta Sqrt(x) radacina patrata Nthroot(x,n) radacina de ordinul n a x Sign(x) valoarea -1 daca x < 0; 1 altfel Rem(x, y) restul impartirii intregi a lui x la y Exp(x) e la puterea x Log(x) logaritmul natural (in baza e) Log10(x)- logaritmul zecimal (baza 10)

  • Functii de rotunjire

    Round(x) rotunjeste pana la cel mai apropiat intreg Fix(x) rotunjeste prin scadere (trunghiaza) pana la

    cel mai apropiat intreg (in cazul numerelor pozitive). In cazul numerelor negative, rotunjeste prin adaos.

    Ex. Round(8.6) = 9; round(-8.6) = -9Fix(8.6) = 8; fix(-8.6) = -8 Floor(x) rotunjeste prin scadere pana la cel mai

    apropiat intreg Ceil(x) rotunjeste prin adaos la cel mai apropiat

    intregEx. Floor(-8.6) = -9; ceil (-8.6) = -8

  • Functii pentru matematica discreta

    Matematica discreta este matematicanumerelor intregi

    Exemplu: factorialul (!) unui numar5! = 1x2x3x4x5 = 120In Matlab factorial(5)Ans = 120

  • Alte exemple

    factor(x) factorizarea numarului x in factori primi12 = 2x2x3= 22 x 3In matlab: factor(x)Ans= 2 2 3 gcd(x, y) cel mai mare divizor comunGcd(10,15)Ans = 5 lcm (x, y) cel mai mic multiplu comun rats(x) reprezinta x ca o fractie nchoosek(n,k) combinari de n luate cate k

  • Permutari

    1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 13!

  • Aranjamente n luate cate m

    n = 4; m =2

  • Combinari de n luate cate m

  • Functii trigonometrice

    180 grade.pi radiani

  • Problema

    Sa se gaseasca rezultanta unui grup de forte ce actioneaza aspra unui balon cu aer

    Gravitatie

    Forta vantului

    Flotabilitate

    F forta totalaFx - forta pe axa XFy - forta pe axa Y

  • Problema(2)

    Suma fortelor pe fiecare axa

    Unghiul sub care actioneaza forta rezultanta

  • Problema(3)Modulul rezultantei

    G = 100NF = 200NV =50N

  • Problema(4) un exemplu de mana

    Se calculeaza componentele fortelor pe fiecare din axe, apoi suma

  • Problema(5) un exemplu de mana

    Se calculeaza unghiul rezultantei si modulul acesteia

  • Problema(6)

    Rezolvarea in Matlab

  • Rezolvarea unei probleme in Matlab

    clear, clc %eliminarea variabilelor definite anterior si curatarea %ecranului%Definim intrarileForce =[100, 200, 50];theta = [-90, +90, +30];%convert angles to radianstheta = theta*pi/180;%Find the x componentsForceX = Force.*cos(theta);%Sum the x componentsForceX_total = sum(ForceX);%Find and sum the y components in the same stepForceY_total = sum(Force.*sin(theta));%Find the resulting angle in radiansresult_angle = atan(ForceY_total/ForceX_total);%Find the resulting angle in degreesresult_degrees = result_angle*180/pi%Find the magnitude of the resulting forceForce_total = ForceX_total/cos(result_angle)

  • Realizati graficul functiei sin(x) pentru

    0x4 Crearea unui sir de 100 de esantioane (valori)

    intre 0 si 4.

    Calcularea sin(.) pentru sirul de valori

    Realizarea graficului

    >>x=linspace(0,4*pi,100);

    >>y=sin(x);

    >>plot(y)0 10 20 30 40 50 60 70 80 90 100

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1

  • Graficul functiei e-x/3sin(x) intre 0x4

    Crearea unui sir de 100 de esantioane (valori) intre 0 si 4.

    .

    Calculate sin(.) of functia sin

    Calculate e-x/3 of the functia exponentiala

    ltiply the arrays y produsul ambelor functii

    >>x=linspace(0,4*pi,100);

    >>y=sin(x);

    >>y1=exp(-x/3);

    >>y2=y*y1;

  • Graficul functiei e-x/3sin(x) intre 0x4

    Inmultim vectorii y si y1 corect (element cu element)

    Realizam graficul functiei>>y2=y.*y1;

    >>plot(y2)

    0 10 20 30 40 50 60 70 80 90 100-0.3

    -0.2

    -0.1

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

  • Grafice

    plot(.)

    stem(.)

    Example:>>x=linspace(0,4*pi,100);>>y=sin(x);>>plot(y)>>plot(x,y)

    Example:>>stem(y)>>stem(x,y)

    0 10 20 30 40 50 60 70 80 90 100-0.3

    -0.2

    -0.1

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

    0 10 20 30 40 50 60 70 80 90 100-0.3

    -0.2

    -0.1

    0

    0.1

    0.2

    0.3

    0.4

    0.5

    0.6

    0.7

  • Grafice

    title(.)

    xlabel(.)

    ylabel(.)

    >>title(This is the sinus function)

    >>xlabel(x (secs))

    >>ylabel(sin(x))0 10 20 30 40 50 60 70 80 90 100

    -1

    -0.8

    -0.6

    -0.4

    -0.2

    0

    0.2

    0.4

    0.6

    0.8

    1This is the sinus function

    x (secs)s

    i

    n

    (

    x

    )

  • Operator (relationali, logici)

    == Equal to ~= Not equal to < Strictly smaller > Strictly greater = Greater than equal to & And operator | Or operator

  • Controlul fluxului in Matlab

    if for while break .

  • Structura de control decizionala (de selectie) Sintaxa comenzii IF

    if (Conditie_1)Comenzi Matlab

Click here to load reader

Embed Size (px)
Recommended