Datalogiens Matematiske Grundlag

Download Report

Transcript Datalogiens Matematiske Grundlag

>>mit_modul
MATLAB
Udførelse af programmer
function r = minf(x)
…
r= size(x)
…
…
minf(a)
…
mit_modul.m
Anders P. Ravn
Institut for Datalogi
Aalborg Universitet
Forår 2003
minf.m
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
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)
2.0000
kvadratrod(1000)
ans =
31.6228
» ans*ans
1000.0000
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;
end;
if fundet, resultat = i; else resultat = 0; end;
[b,c] = findindeks(7,a)
b=0
c= 0
MATLAB-maskinen
Script
» A = [9,7]
ans = …
» B = 25i
ans = …
» sinus(A,6)
…
Procesenhed
Matlab.exe
Program
Workspace
A
B
Lager (memory)
Script
» A = [9,7]
ans = …
Funktionskald
» B = 25i
ans = …
» sinus(A,6)
…
Matlab.exe
function res = sinus(x,n)
Workspace
x init A
n init 6
res init ?
Workspace
A
B
Funktionskald -afslutning
Script
» A = [9,7]
ans = …
» B = 25i
ans = …
» sinus(A,6)
…
Matlab.exe
function res = sinus(x,n)
Workspace
x init A
n init 6
res init ?
Workspace
A
B
ans = res
Funktionskald - rekursion
Script
»f(8)
function r = f(x)
r = f(x-1)
Workspace
x init 8
r init ?
Matlab.exe
function r = f(x)
r = f(x-1)
Workspace
ans = ?
Workspace
x init 7
r init ?
…
Og så …
PRØV DET !
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)' .
1
0.8
plot('sinus(x,17)', [0, 2*pi] )
0.6
0.4
0.2
0
-0.2
Sammenlign:
-0.4
-0.6
-0.8
x = 0: 0.2 : 2*pi;
plot(x, sinus(x,17))
-1
0
1
2
3
4
5
6
Integral
» quad('sin', 0, 2*pi)
ans =
0
function r = sin2(x)
r = sinus(x,2);
» quad('sin2', 0, 2*pi)
ans = 3.1416
» quad('sinus(x,2)', 0, 2*pi)
??? Can not find function 'sinus(x,2)'.
Minimum
» fmin('sin', 0, 2*pi)
ans =
4.7124
» ans/pi
ans =
1.5000
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) ];
1
0.8
0.6
0.4
0.2
0
-0.2
» [t,x] = ode23('difflign', [0,2*pi], [0,1]');
» plot(t,x)
-0.4
-0.6
-0.8
-1
0
1
2
3
4
5
6
7
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.
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.
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.
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
fprintf(red_file,'
gon
m
m
m
Jordens radius: 6386000 m
Refraktionskoefficient:
0.13
Fra Til
V
Sd
ih
sh
gon
m
m
m
S
m
d_H
m
S
m
d_H\r\n');
m\r\n');
Eksempel
…
fprintf(red_file,'%5.0f %5.0f %9.4f %9.3f %9.3f %9.3f %9.3f%9.3f\r\n',linie);
5007 5001 99.8900 307.762
1.640
1.555 307.762
0.623