Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Udførelse af programmer Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2003 >>mit_modul minf.m … minf(a) … mit_modul.m function.

Similar presentations


Presentation on theme: "MATLAB Udførelse af programmer Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2003 >>mit_modul minf.m … minf(a) … mit_modul.m function."— Presentation transcript:

1 MATLAB Udførelse af programmer Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2003 >>mit_modul minf.m … minf(a) … mit_modul.m function r = minf(x) … r= size(x) …

2 Programstrukturer Et program definerer rækkefølgen for at udføre ordrer. sekvens ordre ; ordre … valg if ( ) … end; switch … gentagelse (iteration, løkke) for …, while … kald af underprogram (funktion) f( … ) afbrydelse break, return

3 Funktioner function resultat = kvadratrod(a) % Beregn kvadratrod af a ved bisektion low = 0; high = a; while (high - low) > 2*eps, mid = (high+low)/2; if mid*mid > a, high = mid; else low = mid; end end; resultat = (high+low)/2; kvadratrod(4) kvadratrod(1000) ans = » ans*ans

4 Sammensat returværdi function [fundet,resultat] = findindeks(e,a) % finder index for element e i vektor a hvis det findes fundet = 0; for i=1:length(a), if a(i) == e, fundet = 1; break; end; if fundet, resultat = i; else resultat = 0; end; [b,c] = findindeks(7,a) b = 0 c = 0

5 MATLAB-maskinen Script » A = [9,7] ans = … » B = 25i ans = … » sinus(A,6) … Matlab.exe Workspace A B Lager (memory) Procesenhed Program

6 Funktionskald Script » A = [9,7] ans = … » B = 25i ans = … » sinus(A,6) … Matlab.exe Workspace A B function res = sinus(x,n) Workspace x init A n init 6 res init ?

7 Funktionskald -afslutning Script » A = [9,7] ans = … » B = 25i ans = … » sinus(A,6) … Matlab.exe Workspace A B function res = sinus(x,n) Workspace x init A n init 6 res init ? ans = res

8 Funktionskald - rekursion Script »f(8) Matlab.exe Workspace ans = ? function r = f(x) r = f(x-1) Workspace x init 8 r init ? function r = f(x) r = f(x-1) Workspace x init 7 r init ? …

9 Og så … PRØV DET !

10 Funktionaler (Function-functions) FPLOT(FUN,LIMS) plots the function specified by the string FUN between the x-axis limits specified by LIMS = [XMIN XMAX]. FUN must be the name of an M-file function with variable x such as 'sin(x)', 'diric(x,10)'. plot('sinus(x,17)', [0, 2*pi] ) Sammenlign: x = 0: 0.2 : 2*pi; plot(x, sinus(x,17))

11 Integral » quad('sin', 0, 2*pi) ans = 0 » quad('sinus(x,2)', 0, 2*pi) ??? Can not find function 'sinus(x,2)'. function r = sin2(x) r = sinus(x,2); » quad('sin2', 0, 2*pi) ans =

12 Minimum » fmin('sin', 0, 2*pi) ans = » ans/pi ans =

13 Løsning af Differentialligninger function xdiff = difflign(t,x) % Differentialligningerne % x(1)'(t) = x(2) % x(2)'(t) = -x(1) % xdiff = [ - x(2) ; x(1) ]; » [t,x] = ode23('difflign', [0,2*pi], [0,1]'); » plot(t,x)

14 Simpel Ind- og Udlæsning » help iofun File import/export functions. load - Load workspace from MAT-file. save - Save workspace to MAT-file. dlmread - Read ASCII delimited file. dlmwrite - Write ASCII delimited file. wk1read - Read spreadsheet (WK1) file. wk1write - Write spreadsheet (WK1) file. Command window I/O clc - Clear command window. home - Send cursor home. disp - Display array. input - Prompt for user input. pause - Wait for user response.

15 Formatteret Ind- og Udlæsning File opening and closing. fopen - Open file. fclose - Close file. Formatted file I/O. fscanf - Read formatted data from file. fprintf - Write formatted data to file. fgetl - Read line from file, discard newline character. fgets - Read line from file, keep newline character. input - Prompt for user input.

16 Formater The format specifier : % [- | + | 0] [Field Width][.Precision] Format Character "-" aligns output left (usually, it's right-aligned). "+" outputs a plus sign for positive numbers (usually, it is supressed). “0” outputs leading zeroes. The field width specifies the overall field length. The precision specifies the length of the fractional part for floating point numbers. If omitted, the default is 6. The format character determines the base type for the formatted values: "d": integer value in decimal format. "f": floating point value in fixed format (xxx.yyyyyy). "e": floating point value in scientific format (0.yyyyyye+zzz). "E": floating point value in scientific format (0.yyyyyyE+zzz). "s": String.

17 Eksempel fprintf(red_file,'Jordens radius: %9.0f m\r\n',R); fprintf(red_file,'Refraktionskoefficient: %9.2f\r\n',k); fprintf(red_file,'\r\n'); fprintf(red_file,' Fra Til V Sd ih sh S d_H\r\n'); fprintf(red_file,' gon m m m m m\r\n'); Jordens radius: m Refraktionskoefficient: 0.13 Fra Til V Sd ih sh S d_H gon m m m m m

18 Eksempel … fprintf(red_file,'%5.0f %5.0f %9.4f %9.3f %9.3f %9.3f %9.3f%9.3f\r\n',linie);


Download ppt "MATLAB Udførelse af programmer Anders P. Ravn Institut for Datalogi Aalborg Universitet Forår 2003 >>mit_modul minf.m … minf(a) … mit_modul.m function."

Similar presentations


Ads by Google