5. laborgyakorlat, házik megoldásai

Contents

clear all, close all

1. feladat

format short g
[sugar,magassag]=meshgrid(1:5,2:2:10);
Z=zeros(6);
Z(1,2:6)=1:5;
Z(2:6,1)=2:2:10;
Z(2:6,2:6)=sugar.^2.*magassag*pi
format short
Z =
            0            1            2            3            4            5
            2       6.2832       25.133       56.549       100.53       157.08
            4       12.566       50.265        113.1       201.06       314.16
            6        18.85       75.398       169.65       301.59       471.24
            8       25.133       100.53       226.19       402.12       628.32
           10       31.416       125.66       282.74       502.65        785.4

2. feladat

[oszlop,sor]=meshgrid(1:5,1:5);
A=5-abs(oszlop-sor);
B=10*eye(5)+A;
b=[1:5]';

L=-tril(B,-1);
R=-triu(B,1);
D=diag(diag(B));

BJ=D\(L+R);
fJ=D\b;

BGS=(D-L)\R;
fGS=(D-L)\b;

% Jacobi

format long
x=zeros(5,1);
lepes=0;
hiba=1;

while hiba>=10^-6
    s=x;
    x=BJ*x+fJ;
    hiba=norm(x-s,inf);
    lepes=lepes+1;
end

Jacobi_megoldas=x
Jacobi_iteraciszam=lepes

% Gauss-Seidel

x=zeros(5,1);
lepes=0;
hiba=1;

while hiba>=10^-6
    s=x;
    x=BGS*x+fGS;
    hiba=norm(x-s,inf);
    lepes=lepes+1;
end

GS_megoldas=x
GS=lepes

% Pontos megoldás

x_pontos=B\b
format short

% A Gauss-Seidel volt 6-sor gyorsabb.
Jacobi_megoldas =
  -0.002339124166610
   0.041128699409166
   0.092822152505952
   0.163079918921361
   0.265953558760219
Jacobi_iteraciszam =
    61
GS_megoldas =
  -0.002339342577758
   0.041128330036605
   0.092821750846080
   0.163079566690789
   0.265953277546877
GS =
    10
x_pontos =
  -0.002339410770345
   0.041128350639942
   0.092821782178218
   0.163079570152137
   0.265953272156484