Вы находитесь на странице: 1из 51

20082009

ecrins% matlab -nojvm < M A T L A B > Copyright 1984-2006 The MathWorks, Inc. Version 7.3.0.298 (R2006b) August 03, 2006

This is a Classroom License for instructional use only. Research and commercial use is prohibited. To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> quit ecrins%
1

>> help sin SIN Sine. SIN(X) is the sine of the elements of X.

Overloaded methods help sym/sin.m

>> sin(2) ans = 0.9093 >> SIN(2) ??? Undefined variable or capitalized internal function SIN; Caps Lock may be on.

>> help HELP topics: matlab/general matlab/ops matlab/lang matlab/elmat matlab/elfun matlab/specfun matlab/matfun matlab/datafun matlab/polyfun matlab/funfun matlab/sparfun matlab/graph2d matlab/graph3d matlab/specgraph matlab/graphics matlab/uitools matlab/strfun matlab/iofun matlab/timefun General purpose commands. Operators and special characters. Programming language constructs. Elementary matrices and matrix manipulation. Elementary math functions. Specialized math functions. Matrix functions - numerical linear algebra. Data analysis and Fourier transforms. Interpolation and polynomials. Function functions and ODE solvers. Sparse matrices. Two dimensional graphs. Three dimensional graphs. Specialized graphs. Handle Graphics. Graphical user interface tools. Character strings. File input/output. Time and dates.
2

matlab/datatypes matlab/demos toolbox/compiler images/images images/imdemos toolbox/local nnet/nnet nnet/nndemos toolbox/optim toolbox/signal toolbox/stats toolbox/symbolic toolbox/tour Graphics/MatDraw

Data types and structures. Examples and demonstrations. MATLAB Compiler Image Processing Toolbox. Image Processing Toolbox --- demos and sample images Preferences. Neural Network Toolbox. Neural Network Demonstrations. Optimization Toolbox. Signal Processing Toolbox. Statistics Toolbox. Symbolic Math Toolbox. MATLAB Tour MatDraw GUI Toolbox

For more help on directory/topic, type "help topic".

>> help matfun

>> a=2 a = 2 >> whos Name Size a 1x1

Bytes 8

Class double array

Grand total is 1 elements using 8 bytes >> clear >> whos >>

>> x=[ 1 2 3 ] x = 1 2 3

>> y=[ 2, 3, 4] y = 2 3 4

>> z=[ 5 ; 6 ; 7] z = 5 6 7 >> A=[1 2 ; 5 6 ; 7 8] A = 1 5 7 2 6 8 % Ceci est un commentaire pour souligner leffet de ";" Bytes 48 24 24 24 24 Class double double double double double array array array array array

>> b=[ 1 2 3 ] ; >> whos Name Size A b x y z 3x2 1x3 1x3 1x3 3x1

Grand total is 18 elements using 144 bytes

>> x =

1 >> x+y ans = 3

>> y + [ 4 5 7] ans = 6 >> x=2.6 x = 2.6000 8 11

>> pi ans = 3.1416 >> 1/0 Warning: Divide by zero. ans =

Inf >> ans+1 ans = Inf >> 0/0 Warning: Divide by zero. ans = NaN

>> i ans = 0+ 1.0000i >> sqrt(-1) ans = 0+ 1.0000i >> j ans = 0+ 1.0000i >> j+1 ans = 1.0000+ 1.0000i

>> [ real(2+i) ; imag(2+i)] ans = 2


6

1 >> v=[ 2+i ; 4+7i] v = 2.0000+ 1.0000i 4.0000+ 7.0000i >> [ real(v) imag(v)] ans = 2 4 1 7

>> help isreal ISREAL True for real array. ISREAL(X) returns 1 if all elements in X have zero imaginary part and 0 otherwise. ISREAL(X) detects complex arrays (i.e., arrays that have a non-zero real part). See also REAL, IMAG, I, J.

>> 91/2 ans = 4.5000 >> 9(1/2) ans =

Relational operators. eq - Equal ne - Not equal lt - Less than gt - Greater than le - Less than or equal ge - Greater than or equal Logical operators. and - Logical or - Logical not - Logical xor - Logical any - True if all - True if

== = < > <= >=

AND & OR | NOT EXCLUSIVE OR any element of vector is nonzero all elements of vector are nonzero

>> (1>2) | 1 ans = 1 >>

>> format >> 137/7 ans = 19.5714

% valeur par defaut (short)

>> format long >> 137/7 ans = 19.57142857142857 >> format short e >> 137/7 ans = 1.9571e+01 >> format long e >> 137/7 ans = 1.957142857142857e+01 >>

>> v=[ 10 11 12 ] v = 10 >> v(1) ans = 10 11 12

>> A=[ 1 2 3 ; 4 5 6 ; 7 8 9] A = 1 4 7 >> A(3,2) ans = 8 >> A(2,2)=45 A = 1 4 7 >> A(:) ans = 1 4 7 2 45 8 3 6 9 >> A(5) ans = 45 >> A(1,:) ans = 1 2 3
10

2 5 8

3 6 9

2 45 8

3 6 9

>> A(:,3) ans = 3 6 9 >> A(3,:)=v A = 1 4 10 2 45 11 3 6 12

>> length(v) ans = 3 >> size(A) ans = 3 3 >> [nbligne,nbcol]=size(A) nbligne = 3

nbcol = 3 >> size(A,2) ans = 3

11

>> B=zeros(2,2) B = 0 0 0 0

>> B=zeros(size(A)) B = 0 0 0 0 0 0 >> D=eye(3) D = 1 0 0 1 0 0 >> E=diag(v) E = 10 0 0 0 11 0 0 0 12 0 0 1 0 0 0

>> C=ones(size(B,1),size(B,2),size(A,1)) C(:,:,1) = 1 1 1 1 1 1 1 1 1

C(:,:,2) =
12

1 1 1

1 1 1

1 1 1

C(:,:,3) = 1 1 1 1 1 1 1 1 1 >> alea=rand(4,3) alea = 0.9501 0.2311 0.6068 0.4860 0.8913 0.7621 0.4565 0.0185 0.8214 0.4447 0.6154 0.7919

>> x=[3:7] x = 3 4

% ou x=3:7

>> x=[2:2:14] x = 2 4 6 8 10 12 14

>> x=[0:0.1:1] x = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000

Columns 8 through 11 0.7000 0.8000 0.9000 1.0000

13

>> A=[ 2 3 4; 4 5 6] A = 2 4 >> A ans = 2 3 4 4 5 6 3 5 4 6

>> B=[ 1 2 ; 3 4 ; 8 9] B = 1 3 8 >> B+A ans = 3 6 12 >> A*B ans = 43 52 67 82 >> A*B ??? Error using ==> * Inner matrix dimensions must agree. 6 9 15 % produit de matrices de tailles compatibles 2 4 9

>> 1+A

14

ans = 3 5 4 6 5 7

>> A+ones(size(A)) ans = 3 5 >> B*2 ans = 2 4 6 8 16 18 >> B= eye(2) B = 1 0 0 1 4 5 6 7 % multiplication par un scalaire

>> B=B+1; >> B B = 2 1 >> B2 ans = 5 4 4 5 1 2

>> A A = 2 3 4
15

>> C=[ 1 2 1 ; 1 2 1]; >> A.*C % terme a terme ans = 2 4 >> A.2 ans = 4 9 16 16 25 36 >> A./rand(size(A)) ans = 2.1696 5.4185 17.0197 12.3242 4.2759 6.5438 6 10 4 6 % terme a terme

% terme a terme

>> v=rand(2) v = 0.4103 0.8936 >> v<0.40 ans = 0 1 0 1 >> b=(v >= 0.7); >> cos(b) ans = 1.0000 0.5403 1.0000 1.0000 0.0579 0.3529

>> help min

16

MIN

Smallest component. For vectors, MIN(X) is the smallest element in X. For matrices, MIN(X) is a row vector containing the minimum element from each column. For N-D arrays, MIN(X) operates along the first non-singleton dimension. [Y,I] = MIN(X) returns the indices of the minimum values in vector I. If the values along the first non-singleton dimension contain more than one minimal element, the index of the first one is returned. MIN(X,Y) returns an array the same size as X and Y with the smallest elements taken from X or Y. Either one can be a scalar. [Y,I] = MIN(X,[],DIM) operates along the dimension DIM. Suppose X=[2 8 4;7 3 9] then min(X,[],1) is [2 3 4] and min(X,[],2) is [2,3]. When complex, the magnitude MIN(ABS(X)) is used. NaNs are ignored when computing the minimum. Example: If X = [2 8 4 7 3 9] min(X,[],2) is [2 3], then min(X,[],1) is [2 3 4],

and min(X,5) is [2 5 4 5 3 5].

See also MAX, MEDIAN, MEAN, SORT. >> v= randperm(10) v = 4 1 6 10 8 2 5 9 7 3

>> [vtri,indices]=sort(v) vtri = 1 2 3 4 5 6 7 8 9 10

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

17

>> A=rand(4,4) A = 0.8132 0.0099 0.1389 0.2028 >> A(2:3,3:4) ans = 0.7468 0.4451 >> A(:,2) ans = 0.1987 0.6038 0.2722 0.1988 0.4186 0.8462 0.1987 0.6038 0.2722 0.1988 0.0153 0.7468 0.4451 0.9318 0.4660 0.4186 0.8462 0.5252

>> A([1 3],[4,2]) ans = 0.4660 0.1987 0.8462 0.2722 >> B=A(1:2,:) B = 0.8132 0.1987 0.0099 0.6038 >> B=A(1:2,1:2) B = 0.8132 0.0099 >> B(:) 0.1987 0.6038 0.0153 0.7468 0.4660 0.4186

18

ans = 0.8132 0.0099 0.1987 0.6038 >> [i,j]=find(B>0.5) i = 1 2

j = 1 2 >> l=find(B>0.5) l = 1 4 >> B(l)=ones(size(B(l))) B = 1.0000 0.1987 0.0099 1.0000 >> A(1:2,1:2)=B A = 1.0000 0.0099 0.1389 0.2028 0.1987 1.0000 0.2722 0.1988 0.0153 0.7468 0.4451 0.9318 0.4660 0.4186 0.8462 0.5252

% retour a lenvoyeur apres modifs

>> A=[1 2 3; 4 5 6] A = 1 2 3
19

>> b=reshape(A,3,2) b = 1 4 2 5 3 6

v = 1 >> l=[v v] l = 1 >> m=[] m = [] >> m=[m; [ 3 4]] m = 3 4 2 3 1 2 3 2 3

>> m=[m; [ 3 5]] m = 3 4 3 5 >> A=rand(2,2); >> B=A+rand; >> A A = 0.2987 0.6614 0.2844 0.4692
20

>> B B = 0.3635 0.7262 >> C=[A B] C = 0.2987 0.2844 0.6614 0.4692 a11=rand(2,2); a12=rand(2,2); a21=rand(3,2); a22=rand(3,2); ma=[ a11 a12 ; ... a21 a22] 0.3635 0.7262 0.3492 0.5340 0.3492 0.5340

>> >> >> >> >>

ma = 0.8214 0.4447 0.9355 0.9169 0.4103 0.6154 0.7919 0.8936 0.0579 0.3529 0.9218 0.7382 0.8132 0.0099 0.1389 0.1763 0.4057 0.2028 0.1987 0.6038

>> A=rand(3,3) A =
21

0.3046 0.1897 0.1934 >> b=rand(3,1) b = 0.8600 0.8537 0.5936 >> x=A\b x = 3.9958 -0.6127 0.4031 >> A*x-b ans = 1.0e-15 * 0.1110 0.1110 0.1110

0.6822 0.3028 0.5417

0.1509 0.6979 0.3784

22

x = 0 >> y=2*x+4 y = 4 6 8 10 12 14 1 2 3 4 5

>> plot(x,y,r+) >> hold on >> plot(x,y)

14

13

12

11

10

0.5

1.5

2.5

3.5

4.5

>> >> >> >>

grid xlabel(x) ylabel(axe y) title( mon affichage);

>> z=rand(5,5) z =

23

0.5341 0.7271 0.3093 0.8385 0.5681 >> mesh(z)

0.3704 0.7027 0.5466 0.4449 0.6946

0.6213 0.7948 0.9568 0.5226 0.8801

0.1730 0.9797 0.2714 0.2523 0.8757

0.7373 0.1365 0.0118 0.8939 0.1991

0.8

0.6

0.4

0.2

0 5 4 3 3 2 1 1 2 4 5

>> >> >> >> >> >> >> >> >> >> >> >>

x=-pi:pi/10:pi; y=x; [X,Y]=meshgrid(x,y); Z=(-X.2+Y.2/4)/2; figure mesh(X,Y,Z); xlabel(x) ylabel(y) zlabel(z) title(paraboloide hyperbolique : 2z=(-x2+y2/4)); hold on plot3(X,Y,Z,k*)

24

paraboloide hyperbolique : 2z=(x +y /4)

2 1 0 1 z 2 3 4 5 4 2 0 0 2 y 4 4 2 x 2 4

SUBPLOT Create axes in tiled positions. SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc. For example, SUBPLOT(2,1,1), PLOT(income) SUBPLOT(2,1,2), PLOT(outgo) plots income on the top half of the window and outgo on the bottom half.

>> >> >> >> >>

figure subplot(2,2,1),plot3(X,Y,Z,b+) subplot(2,2,2),plot(X,Z,b+) subplot(2,2,3),mesh(X,Y,Z) subplot(2,2,4),surf(X,Y,Z)

25

2 5 1 0 0 1 2 5 5 0 5 5 0 3 5 4 5 4 2 0 2 4

5 5 0 5 5 0

5 5 0 5 5 0

>> a=[ 2 3 4 ; ... 5 6 7 ] a = 2 5 3 6 4 7

>> a=10; b=56;

26

>> a = 4 a = 4 >> b= 6 b = 6 >> save toto a b >> ls % ou dir pour examiner le contenu du repertoire courant ans = toto.mat

>> >> >> >>

clear who load toto who

Your variables are: a >> b

MATLAB char uchar schar int8

C or Fortran char*1 unsigned char signed char integer*1


27

Description character, 8 bits unsigned character, 8 bits signed character, 8 bits integer, 8 bits.

int16 int32 int64 uint8 uint16 uint32 uint64 float32 float64 >>

integer*2 integer*4 integer*8 integer*1 integer*2 integer*4 integer*8 real*4 real*8 help diary

integer, integer, integer, unsigned unsigned unsigned unsigned floating floating

16 bits. 32 bits. 64 bits integer, 8 bits. integer, 16 bits. integer, 32 bits. integer, 64 bits point, 32 bits. point, 64 bits.

DIARY Save text of MATLAB session. DIARY file_name causes a copy of all subsequent terminal input and most of the resulting output to be written on the named file. 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 a string.

>> monscript

28

>> help function FUNCTION Add new function. New functions may be added to MATLABs vocabulary if they are expressed in terms of other existing functions. The commands and functions that comprise the new function must be put in a file whose name defines the name of the new function, with a filename extension of .m. At the top of the file must be a line that contains the syntax definition for the new function. For example, the existence of a file on disk called STAT.M with: function [mean,stdev] = stat(x) n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).2)/n); defines a new function called STAT that calculates the mean and standard deviation of a vector. The variables within the body of the function are all local variables. See SCRIPT for procedures that work globally on the workspace. Normally functions return when the end of the function is reached. A RETURN statement can be used to force an early return. See also SCRIPT, RETURN, VARARGIN, VARARGOUT, NARGIN, NARGOUT, INPUTNAME.

29

fonc.m monscript.m function [d,e]=fonc(pformel) a=3; % locale par defaut global a a=2; [b,c]=fonc(a); %5,2 [b,c]=fonc2(a); %4,1 fonc2.m function [d,e]=fonc2(pformel) global a d=a+pformel; e=a-1; d=a+pformel; e=a-1;

IF IF statement condition. The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END The statements are executed if the real part of the expression has all non-zero elements. The ELSE and ELSEIF parts are optional. Zero or more ELSEIF parts can be used as well as nested IFs. The expression is usually of the form expr rop expr where rop is ==, <, >, <=, >=, or =. Example if I == J A(I,J) = 2;
30

elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end

FOR

Repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., statement END The columns of the expression are stored one at a time in the variable and then the following statements, up to the END, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars. Some examples (assume N has already been assigned a value). FOR I = 1:N, FOR J = 1:N, A(I,J) = 1/(I+J-1); END END FOR S = 1.0: -0.1: 0.0, END steps S with increments of -0.1 FOR E = EYE(N), ... END sets E to the unit N-vectors. The BREAK statement can be used to terminate the loop prematurely.

Un exemple de boucle while : E = 0*A; F = E + eye(size(E)); N = 1; while norm(E+F-E,1) > 0, E = E + F; F = A*F/N; N = N + 1; end

31

>> chaine=bonjour monsieur chaine = bonjour monsieur >> chaine(2)=i chaine = binjour monsieur >> v=[bonjour monsieur] v = bonjourmonsieur v=[bonjour monsieur] v = bonjour monsieur >> v==chaine ans = Columns 1 through 12 1 0 1 1 1 1 1 1 1 1 1 1

Columns 13 through 16 1 1 1 1

>> v= janvier v =
32

janvier >> u=[v 8 ; tonton] ??? All rows in the bracketed expression must have the same number of columns. >> u=[v 8 ; tonton 9] u = janvier8 tonton 9

>> help num2str NUM2STR Convert number to string. T = NUM2STR(X) converts the matrix X into a string representation T with about 4 digits and an exponent if required. This is useful for labeling plots with the TITLE, XLABEL, YLABEL, and TEXT commands. T = NUM2STR(X,N) converts the matrix X into a string representation with a maximum N digits of precision. The default number of digits is based on the magnitude of the elements of X. T = NUM2STR(X,FORMAT) uses the format string FORMAT (see SPRINTF for details). Example: num2str(randn(2,2),3) produces the string matrix -0.433 -1.67 0.125 0.288

>> eval(1+2) ans = 3 >> x=3 x = 3


33

>> expression=x+7 expression = x+7 >> eval(expression) ans = 10 >> feval(zeros,2,2) ans = 0 0 0 0

>> A=[0 0 1 ; 1 0 2 ; 0 -3 0] A = 0 1 0 0 0 -3 1 2 0

>> S=sparse(A) S = (2,1) (3,2) (1,3) (2,3) >> whos Name A S 1 -3 1 2 Size 3x3 3x3 Bytes 72 64 Class double array sparse array

Grand total is 13 elements using 136 bytes


34

>> A=sparse(3,2) A = All zero sparse: 3-by-2 >> A(1,2)=9 A = (1,2) >> A(3,2)=5 A = (1,2) (3,2) >> 9 5 9

function fx = f(x) fx.Value=(x(1)-1)2+x(1)*x(2); fx.Gradient = [2*(x(1)-1)+x(2); x(1)]; fx.Hessien = [2 1 ; 1 0];

35

>> x = [2;1] x = 2 1

>> f(x) ans = Value: 3 Gradient: [2x1 double] Hessien: [2x2 double] >> whos Name ans x

Size 1x1 2x1

Bytes 428 16

Class struct array double array

Grand total is 12 elements using 444 bytes>> gx=f(x); >> gx gx = Value: 3 Gradient: [2x1 double] Hessien: [2x2 double] >> fieldnames(gx) ans = Value Gradient Hessien >> gx.Value=9 gx = Value: 9 Gradient: [2x1 double] Hessien: [2x2 double]

%affectation

36

>> A{1}=x; >> A{2}=gx; >> A A = [2x1 double] >> B={x,gx,4} B = [2x1 double] >> s={{},{8},9} s = {} >> s{2} ans = [8] {1x1 cell} [9] [1x1 struct] [4] [1x1 struct]

37

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

ptr = mxGetPr(prls[i]) ptr=mxGetPi(prls[i]) m=mxGetM(prls[i]) m=mxGetN(prls[i]) mat=mxCreateDoubleMatrix(m,n,mxReal) mxCalloc mxMalloc mxFree mxDestryArray

renvoie un pointeur sur le parametre i (partie reelle) renvoie un pointeur sur le parametre i (partie imaginaire) nombre de ligne du parametre i nombre de colonne du parametre i creation dune matrice de reels double precision allocation liberation despace memoire
38

mxIs{Double,Char,COmplex,Real) mexPrintf

test de type afchage

void mexCallMATLAB( int nlhs, const mxArray *prhs[],char *fun)

mxArray *plhs[],int nrhs,

% % Test de la fonction exemple_mex compilee avec mex % pour compiler exemple_mex.c en exemple_mex.mexsol % on utilise la commande mex -inline exemple_mex.c % IN = zeros(4,3); [OUT1,OUT2,OUT3]=exemple_mex(IN); IN OUT1 OUT2 OUT3

/* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /*

Fichier dexemple dutilisation dun fichier C destine a etre compile en me La fonction est appellee sous MATLAB de la maniere suivante :*/ [OUT1,OUT2,OUT3]=exemple_mex(IN), avec OUT1, OUT2, OUT3 et IN de meme taill Le fonctionnement de linterfacage entre les parametres MATLAB et les param 4 parametres sont passees a la fonction mexFunction */ void mexFunction( int nlhs, mxArray *plhs[],*/ int nrhs, const mxArray *prhs[]*/ ) */ nlhs : nombre de parametres en sortie */ nrhs : nombre de parametres en entree */ plhs[i] : ieme parametre en sortie */ prhs[i] : ieme parametre en entree */ La routine mxGetPr permet de recuperer un pointeur vers un des parametres : ptr_in_i = mxGetPr(prhs[i]); : pointeur vers le ieme parametre en entree */ Attention, le type de ptr_in_i doit etre un pointeur sur un type compatible Cas des matrices : */ Les matrices sont converties lors du passage de parametre de MATLAB vers C
39

/* /* /* /* /* /* /* /* /* /*

deux cas dutilisation sont presentes : */ utilisation du vecteur et utilisation dune matrice intermediaire */ Fonctionnement de la routine testee : */ [OUT1,OUT2,OUT3] = exemple_mex(IN) avec IN:4*3, OUT1:4*3, OUT2:4*3, OUT3:4* OUT_1=IN+1+increment ; */ OUT_2=IN+1+increment ; */ OUT_3=IN+1+increment ; */ Si on supprime la declaration de matrices statiques */ les tailles des matrices en parametre peuvent etre */ quelconques */

#include <math.h> #include "mex.h"

#define #define #define #define

VECT_IN prhs[0] /* VECT_OUT_1 plhs[0] VECT_OUT_2 plhs[1] VECT_OUT_3 plhs[2]

Pointeur vers le /* Pointeur vers /* Pointeur vers /* Pointeur vers

premier parametre en entree */ le premier parametre en sortie */ le deuxieme parametre en sortie */ le troisieme parametre en sortie *

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double double double double double double double double *vect_in; /* Pointeur vers le vecteur interne dentree */ *vect_out_1;/* Pointeur vers le premier vecteur de sortie */ *vect_out_2;/* Pointeur vers le second vecteur de sortie */ *vect_out_3;/* Pointeur vers le troisieme vecteur de sortie */ mat_in_aux[4][3]; /* Matrice intermediaire en entree */ mat_out_aux[4][3]; /* Matrice intermediaire en sortie */ **mat_in_aux_dyn; /* Matrice allouee dynamiquement en entree */ **mat_out_aux_dyn; /* Matrice allouee dynamiquement en sortie */

int m,n; int i,j;

/********************************************/ /* Recuperation de la taille des matrices */ /* Nombre de lignes du tableau en entree */ m=mxGetM(VECT_IN); /* Nombre de colonnes du tableau en entree */ n=mxGetN(VECT_IN);
40

/* le pointeur pointe vers le vecteur MATLAB passe en entree */ vect_in = mxGetPr(VECT_IN);

/********************************************/ /* Utilisation du vecteur */ /* Creation dun vecteur de taille mn en sortie */ VECT_OUT_1 = mxCreateDoubleMatrix(m,n,mxREAL); /* le premier pointeur pointe vers le vecteur MATLAB passe en sortie */ vect_out_1 = mxGetPr(VECT_OUT_1); /* Passage du vecteur dentree au vecteur de sortie */ for(i=0;i<m*n;i++) vect_out_1[i] = vect_in[i]+1+i; /********************************************/ /* Utilisation de la matrice intermediaire */ /* Creation dun vecteur de taille mn en sortie */ VECT_OUT_2 = mxCreateDoubleMatrix(m,n,mxREAL); /* le deuxieme pointeur pointe vers le vecteur MATLAB passe en sortie */ vect_out_2 = mxGetPr(VECT_OUT_2); /* Passage du vecteur dentree a la matrice dentree */ for(j=0;j<n;j++) for(i=0;i<m;i++) mat_in_aux[i][j]=vect_in[j*m+i]; /* Passage de la matrice dentree a la matrice de sortie */ for(i=0;i<m;i++) for(j=0;j<n;j++) mat_out_aux[i][j]=mat_in_aux[i][j]+2; /* Passage de la matrice de sortie au vecteur de sortie */ for(j=0;j<n;j++) for(i=0;i<m;i++) vect_out_2[j*m+i]=mat_in_aux[i][j]+1+j*m+i; /********************************************/ /* Utilisation de lallocation dynamique */ /* La matrice pointe vers un vecteur de pointeurs sur des doubles */ mat_in_aux_dyn= malloc(m*sizeof(double*)); for(i=0;i<m;i++) /* Chaque element du vecteur precedent pointe vers une ligne de doubles*/
41

mat_in_aux_dyn[i] = malloc(n*sizeof(double)); /* La matrice pointe vers un vecteur de pointeurs sur des doubles */ mat_out_aux_dyn= malloc(m*sizeof(double*)); for(i=0;i<m;i++) /* Chaque element du vecteur precedent pointe vers une ligne de doubles*/ mat_out_aux_dyn[i] = malloc(n*sizeof(double)); /* Creation dun vecteur de taille mn en sortie */ VECT_OUT_3 = mxCreateDoubleMatrix(m,n,mxREAL); /* le troisieme pointeur pointe vers le vecteur MATLAB passe en sortie */ vect_out_3 = mxGetPr(VECT_OUT_3); /* Passage du vecteur dentree a la matrice dentree */ for(j=0;j<n;j++) for(i=0;i<m;i++) mat_in_aux_dyn[i][j]=vect_in[j*m+i]; /* Passage de la matrice dentree a la matrice de sortie */ for(i=0;i<m;i++) for(j=0;j<n;j++) mat_out_aux_dyn[i][j]=mat_in_aux_dyn[i][j]+2; /* Passage de la matrice de sortie au vecteur de sortie */ for(j=0;j<n;j++) for(i=0;i<m;i++) vect_out_3[j*m+i]=mat_in_aux_dyn[i][j]+1+j*m+i; return; }

SUBROUTINE MEXFUNCTION(NLHS,PLHS,LRHS,PRHS) INTEGER*4 NLHS,NRHS INTEGER*4 PLHS(*), PRHS(*)

42

% % Test de la fonction exemple_mex compilee avec mex % pour compiler exemple_mex.c en exemple_mex.mexsol % on utilise la commande mex -inline exemple_mex.c % IN = zeros(4,3); for i=1:4, for j=1:3, IN(i,j) = rand(1); end end

% on aurait mieux fait dutiliser rand(4,3) !

[OUT]=exemple_mex_g(IN); IN OUT

SUBROUTINE EXEMPLE_MEX(OUT,IN,M,N) REAL*8 OUT(4,3), IN(4,3) INTEGER*4 M,N INTEGER*4 I, J DO 10 I=1,M DO 20 J=1,N OUT(I,J) = IN(I,J)+I WRITE(*,100) OUT(I,J) 100 FORMAT(F7.5) 20 CONTINUE 10 CONTINUE RETURN END

43

C C SUBROUTINE MEXFUNCTION(NLHS, PLHS, NRHS, PRHS) C----------------------------------------------------C (pointer) Replace integer by integer*8 on the DEC Alpha and C the SGI 64-bit platforms C INTEGER*4 PLHS(*), PRHS(*) C----------------------------------------------------C INTEGER*4 NLHS, NRHS C C----------------------------------------------------C (pointer) Replace integer by integer*8 on the DEC Alpha and C the SGI 64-bit platforms C INTEGER*4 MXCREATEFULL, MXGETPR C----------------------------------------------------C INTEGER*4 MXGETM, MXGETN C C KEEP THE ABOVE SUBROUTINE, ARGUMENT, AND FUNCTION DECLARATIONS C FOR USE IN ALL YOUR FORTRAN MEX FILES. C----------------------------------------------------C C----------------------------------------------------C (pointer) Replace integer by integer*8 on the DEC Alpha and C the SGI 64-bit platforms C INTEGER*4 OUT, IN C----------------------------------------------------C INTEGER*4 M, N

44

C M = MXGETM(PRHS(1)) N = MXGETN(PRHS(1)) C C CREATE A MATRIX FOR RETURN ARGUMENT C PLHS(1) = MXCREATEFULL(M,N,0) C C ASSIGN POINTERS TO THE VARIOUS PARAMETERS C OUT = MXGETPR(PLHS(1)) C IN = MXGETPR(PRHS(1)) C C DO THE ACTUAL COMPUTATIONS IN A SUBROUTINE C CREATED ARRAYS. C CALL EXEMPLE_MEX(%VAL(OUT),%VAL(IN),M,N) C C COPY OUTPUT WHICH IS STORED IN LOCAL ARRAY TO MATRIX OUTPUT C RETURN END

>> help sym --- help for sym/sym.m --SYM Construct symbolic numbers, variables and objects. S = SYM(A) constructs an object S, of class sym, from A. If the input argument is a string, the result is a symbolic number or variable. If the input argument is a numeric scalar or matrix, the result is a symbolic representation of the given numeric values.

x = sym(x) creates the symbolic variable with name x and stores the result in x. alpha = sym(alpha) and r = sym(Rho) are other examples. etc... >> a=sym(x) a = x >> whos
45

Name a

Size 1x1

Bytes 126

Class sym object

Grand total is 2 elements using 126 bytes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Calcul Symbolique %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% x = sym ([x(1) x(2) x(3)])

% Copier - Coller a partir de lexpression de la fonction % de Brown interpretable en MATLAB fdex = ... sqrt( (x(1)2) +(x(2)2) +(x(2)2) +(x(3)2) )

(x(2)2 (x(1)2 (x(3)2 (x(2)2

+ + + +

1) 1) 1) 1)

... ... ... ...

% On transforme les x(i) en xi grace au "subs" syms = subs x1 x2 x3 (fdex , {sym(x(1)), sym(x(2)),sym(x(3))} , {x1,x2,x3})

fdex

dfdex = jacobian (fdex ,[x1,x2,x3]) % On transforme les xi en x(i) grace au "subs" dfdex = subs inverse

(dfdex,{x1,x2,x3} , {sym(x(1)), sym(x(2)),sym(x(3))})

% Et voici le resultat que lon peut copier-coller dans la fonction grad_brown % simplify transforme log(xi 2) en 2*log(xi)!!!

dfdex = ... [1/2/((x(1)2)(x(2)2+1)+(x(2)2)(x(1)2+1)+(x(2)2)(x(3)2+1)+ (x(3)2)(x(2)2+1))(1/2)*(2*(x(1)2)(x(2)2+1)*(x(2)2+1)/x(1)+ 2*(x(2)2)(x(1)2+1)*x(1)*log(x(2)2)), 1/2/((x(1)2)(x(2)2+1)+(x(2)2)(x(1)2+1)+(x(2)2)(x(3)2+1)+ (x(3)2)(x(2)2+1))(1/2)*(2*(x(1)2)(x(2)2+1)*x(2)*log(x(1)2)+ 2*(x(2)2)(x(1)2+1)*(x(1)2+1)/x(2)+ 2*(x(2)2)(x(3)2+1)*(x(3)2+1)/x(2)+2*(x(3)2)(x(2)2+1)*x(2)*log(x(3)2)), 1/2/((x(1)2)(x(2)2+1)+(x(2)2)(x(1)2+1)+(x(2)2)(x(3)2+1)+ (x(3)2)(x(2)2+1))(1/2)*(2*(x(2)2)(x(3)2+1)*x(3)*log(x(2)2)+ 2*(x(3)2)(x(2)2+1)*(x(2)2+1)/x(3))]
46

pretty

(dfdex)

>> help menu MENU Generate a menu of choices for user input. K = MENU(Choose a color,Red,Blue,Green) displays on the screen: ----- Choose a color ----1) Red 2) Blue 3) Green Select a menu number: The number entered by the user in response to the prompt is returned. On machines that support it, the local menu system is used. The maximum number of menu items is 32. See also UIMENU.

47

>> help uitools Graphical user interface tools. GUI functions. uicontrol - Create user interface control. uimenu - Create user interface menu. ginput - Graphical input from mouse. dragrect - Drag XOR rectangles with mouse. rbbox - Rubberband box. selectmoveresize - Interactively select, move, resize, or copy objects. waitforbuttonpress - Wait for key/buttonpress over figure. waitfor - Block execution and wait for event. uiwait - Block execution and wait for resume. uiresume - Resume execution of blocked M-file. GUI design tools. guide - Design GUI. align - Align uicontrols and axes. cbedit - Edit callback. menuedit - Edit menu. propedit - Edit property. Dialog boxes. dialog axlimdlg errordlg helpdlg inputdlg listdlg menu msgbox questdlg warndlg uigetfile uiputfile uisetcolor uisetfont pagedlg printdlg waitbar -

Create dialog figure. Axes limits dialog box. Error dialog box. Help dialog box. Input dialog box. List selection dialog box. Generate menu of choices for user input. Message box. Question dialog box. Warning dialog box. Standard open file dialog box. Standard save file dialog box. Color selection dialog box. Font selection dialog box. Page position dialog box. Print dialog box. Display wait bar.

Menu utilities. makemenu - Create menu structure. menubar - Computer dependent default setting for MenuBar property. umtoggle - Toggle "checked" status of uimenu object. winmenu - Create submenu for "Window" menu item.
48

Toolbar button btngroup btnstate btnpress btndown btnup User-defined clruprop getuprop setuprop

group utilities. Create toolbar button group. Query state of toolbar button group. Button press manager for toolbar button group. Depress button in toolbar button group. Raise button in toolbar button group.

figure/axes property utilities. - Clear user-defined property. - Get value of user-defined property. - Set user-defined property.

49

Вам также может понравиться