---
# System prepended metadata

title: Laboratorium 7 - Kamil Breguła

---

# Laboratorium 7 - Kamil Breguła

Zadanie 1,2
```
syms y(x)
dsolve(diff(y) == x+y, y(0) == 1)

dsolve(diff(y) == x^2 - y, y(0) == 1)


[x1a, y1a] = ode23(@(x, y) x + y, [0 20], 0.01)
[x1b, y1b] = ode45(@(x, y) x + y, [0 20], 0.01)
[x2a, y2a] = ode23(@(x, y) x^2 - y, [0 20], 0.01)
[x2b, y2b] = ode45(@(x, y) x^2 - y, [0 20], 0.01)

hold on
plot(x1a, y1a, 's')
plot(x1b, y1b, 'o')

xxx = 0:0.01:20
plot(xxx, 2*exp(xxx) - xxx - 1)

legend('ode23', 'ode45', 'original')
hold off

clf;
cla;
hold on
plot(x2a, y2a, 's')
plot(x2b, y2b, 'o')

xxx = 0:0.01:20
plot(xxx, xxx.^2 - exp(-xxx) - 2*xxx + 2)

legend('ode23', 'ode45', 'origina')
hold off
```
Zadanie 3
```
h = 0.01
fn = @(x) exp(x^2 / 4)
f = @(x, y) 1/2 * x * y 

yy = [1]
xx = [0]
ya = [1]
for i = 2:100
    x_1 = xx(i-1) + h;
    y_1 = yy(i-1) + h * f(x_1, yy(i-1));
    xx = [xx x_1];
    yy = [yy y_1];
    ya = [ya fn(x_1)];
    if x_1 > 20
        break;
    end;
end

yy_h1 = yy;
ya_h1 = ya;

cla
hold on
plot(xx, ya)
plot(xx, yy, 's-')
legend('ya', 'yy')
title('Wyniki')
hold off

clf;
cla;

title('Bledy')
hold on
e = abs(yy - ya)
plot(xx, abs(yy - ya))

legend('e')
hold off
```
Zadanie 4

```
h = 0.01
fn = @(x) exp(x^2 / 4)
f = @(x, y) 1/2 * x * y 

yy = [1]
xx = [0]
ya = [1]
for i = 2:100
    x_1 = xx(i-1) + h;
    y_1 = yy(i-1) + (1/2 * h) * f(x_1, yy(i-1));
    y_1 = yy(i-1) + h * f(xx(i-1) + h, y_1);
    xx = [xx x_1];
    yy = [yy y_1];
    ya = [ya fn(x_1)];
    if x_1 > 20
        break;
    end;
end

yy_h1 = yy;
ya_h1 = ya;

cla; clf
hold on
plot(xx, ya)
plot(xx, yy, 's-')
legend('ya', 'yy') 
title('Wyniki')
hold off

clf;
cla;

title('Bledy')
hold on
e = abs(yy - ya)
plot(xx, abs(yy - ya))

legend('e')
hold off
```