Klausur - Lösung

Contents

Called Functions

Mit Tests für jeden Aufgabenteil. Lösung ist am Ende zu finden.

A1a)

clear all; close all;

disp(aufgabe1(4))
disp(aufgabe1(5.8))
disp(aufgabe1(6.1))
    -2     1     0     1
     1    -2     1     0
     0     1    -2     1
     1     0     1    -2

Achtung: Nur natürliche Zahlen sind zulässig.
         Runde ab.
    -2     1     0     0     1
     1    -2     1     0     0
     0     1    -2     1     0
     0     0     1    -2     1
     1     0     0     1    -2

Achtung: Nur natürliche Zahlen sind zulässig.
         Runde ab.
    -2     1     0     0     0     1
     1    -2     1     0     0     0
     0     1    -2     1     0     0
     0     0     1    -2     1     0
     0     0     0     1    -2     1
     1     0     0     0     1    -2

A2a)

clear all; close all;
f=@(x) 71-6*x^2+0.4*x^3;

x0=0;x1=3;
[x,n,err]=aufgabe2(f,x0,x1,1.e-12);
disp(['x = ' num2str(x) ',  n = ' num2str(n) ',  err = ' num2str(err)])

x0=10;x1=13;
[x,n,err]=aufgabe2(f,x0,x1,1.e-12);
disp(['x = ' num2str(x) ',  n = ' num2str(n) ',  err = ' num2str(err)])

x0=-100;x1=-102;
[x,n,err]=aufgabe2(f,x0,x1,1.e-12);
disp(['x = ' num2str(x) ',  n = ' num2str(n) ',  err = ' num2str(err)])

disp(' ')
x = 4.0208,  n = 6,  err = 6.97e-11
x = 14.1082,  n = 8,  err = 1.3678e-13
x = -3.129,  n = 16,  err = 1.684e-09
 

b)

help aufgabe2
  [x, n, e] = aufgabe2(f,x0,x1,tol)
  Funktion zur Iterativen Bestimmung von Nullstellen.
  Abbruch nach max. 1000 Iterationen.
  Input: f      => Funktion
         x0, x1 => Startwerte
         tol    => Toleranz
  Output: x => Nullstelle
          n => Anz. Itarationen
          e => Fehler

    Published output in the Help browser
       showdemo aufgabe2

A3a)

clear all; close all;

disp(aufgabe3a(44,4))
  -8.4004e+18

b)

disp(aufgabe3b(43,-6))
   1.3210e+18

c)

aufgabe3c

A4a)

clear all; close all;

disp(aufgabe4a(6,5))
       35429

b)

disp(aufgabe4b(6,5))
        5905

c)

clear all; close all;

aufgabe4c

A5a)

name = aufgabe5a('vornamen.txt');
disp([name{2} ' ' name{4} ' ' name{5}])
Wolfgang Werner Klaus

b)

clear all; close all;

aufgabe5b

disp(' ')
% open the file

fid  = fopen('liste.txt');

tline = fgetl(fid);
i=1;
while ischar(tline)
    if 1 == mod(i,8)
        disp([sscanf(tline, '%s%')  ' ' sscanf(tline, '%*s %s%') ' '...
            sscanf(tline, '%*s %*s %s%') ' ' sscanf(tline, '%*s %*s %*s %s')])
    end
    tline = fgetl(fid);
    i=i+1;
end

% close the file
fclose(fid);
 
Student/in Mat.Nr. Note 
Schulz Peter 866930 1.3
Wagner Wolfgang 484480 1.7
Weber Michael 694752 3.7
Schneider Werner 451739 3.0
Bauer Werner 126500 1.3
Schulz Klaus 913800 3.3
Wagner Maria 154370 2.3
Weber Ursula 558319 3.3
Schneider Monika 743688 1.3
Bauer Monika 679734 1.0
Schulz Petra 163512 4.0
Wagner Elisabeth 317428 3.7
aufgabe1
function M = aufgabe1(n)
% Teste ob ganze Zahl.
if n~=floor(n)
    n=floor(n);
    disp('Achtung: Nur natürliche Zahlen sind zulässig.')
    disp('         Runde ab.')
end
% Teste ob >0
if n<1
    error('Es muss ein n>0 gewaelt werden.')
end
% Erzeuge Matrix.
d = -2*ones(n,1);
nd = ones(n-1,1);

M = diag(d,0) + diag(nd,-1) + diag(nd,1);
M(n,1)=1;
M(1,n)=1;
aufgabe2
function [x, n, e] = aufgabe2(f,x0,x1,tol)
% [x, n, e] = aufgabe2(f,x0,x1,tol)
% Funktion zur Iterativen Bestimmung von Nullstellen.
% Abbruch nach max. 1000 Iterationen.
% Input: f      => Funktion
%        x0, x1 => Startwerte
%        tol    => Toleranz
% Output: x => Nullstelle
%         n => Anz. Itarationen
%         e => Fehler
n    = 0;
xn   = x1;
xn_1 = x0;

while ( abs(f(xn))>tol && abs(xn-xn_1)>tol && n<1000)
    tmp = xn - (xn-xn_1)/(f(xn)-f(xn_1))*f(xn);
    xn_1 = xn;
    xn   = tmp;
    n    = n+1;
    e    = abs(xn-xn_1);
end
x = xn;
end
aufgabe3a
function z = aufgabe3a(x,y)
z = real(exp(x+y*1i));
aufgabe3b
function z = aufgabe3b(x,y)
z = imag(exp(x+y*1i));
Aufgabe 3c

Aufgabe 3c

m=40;
x = linspace(-3,3,m/2);
y = linspace(-10,6,m);

figure;
subplot(1,2,1);

[X,Y] = meshgrid(x,y);
surf(X,Y,aufgabe3a(X,Y))
title('Re(e)')
axis([-3 3 -9 6 -20 20])

subplot(1,2,2);
surf(X,Y,aufgabe3b(X,Y))
axis([-3 3 -9 6 -20 20])

title('Im(e)')
aufgabe4a
function y = aufgabe4a(x,k)
if k==0
    y=ones(size(x));
elseif k==1
    y = 2+0.5*x;
else
   y = (2+x).*aufgabe4a(x,k-1)+(0.5*x).^2.*aufgabe4a(x,k-2);
end
aufgabe4b
function y = aufgabe4b(x,k)
y = zeros(size(x));
y0 = y;
if k>0
    y = y + 1;
    y1 = y;
end
for n=2:k
    y = (2+x).*y1+(0.5*x).^2.*y0;
    y0 = y1;
    y1 = y;
end
Aufgabe 4c)

Aufgabe 4c)

close all
x = linspace(-3,0,30);

a2 = aufgabe4a(x,2);
a4 = aufgabe4a(x,4);
b2 = aufgabe4b(x,2);
b4 = aufgabe4b(x,4);

plot(x,a2,'r',x,a4,'b',x,b2,'k--',x,b4,'m--')
legend('a_2(x)','a_4(x)','b_2(x)','b_4(x)',2)
aufgabe5a
function name = aufgabe5a(datei)

% open the file
fid  = fopen(datei);

tline = fgetl(fid);
i=1;
while ischar(tline)
    name{i} = sscanf(tline, '%s%');
    tline = fgetl(fid);
    i=i+1;
end

% close the file
fclose(fid);
aufgabe5b
clear all
filename = 'vornamen.txt';
filename2= 'nachnamen.txt';

vor_n = aufgabe5a(filename);
nach_n= aufgabe5a(filename2);

k=1;
for i = 1:length(vor_n)
    for j = 1:length(nach_n)
        student(k).name = [nach_n{j} ' ' vor_n{i}];
        k=k+1;
    end
end

note = [1.0 1.3 1.7 2.0 2.3 2.7 3.0 3.3 3.7 4.0 5.0];
for i = 1:length(student)
    student(i).mat = round(rand*1.e+6);
    student(i).note = note(1+round(rand*10));
end

filename = 'liste.txt';

% open the file
fid = fopen(filename,'w');
fprintf(fid,'Student/in  Mat.Nr.   Note \n');
for i =1:length(student)
    fprintf(fid,'%s  %6i  %2.1f \n',...
        student(i).name,student(i).mat,student(i).note);
end
% close the file
fclose(fid);