blob: 7cf519d87cecad021f1440c829bf50705aa4f6f4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//clc()
//dy/dx = -2*x^3 + 12*x^2 - 20*x + 8.5
//therefore, y = -0.5*x^4 + 4*x^3 - 10*x^2 + 8.5 + c
x1 = 0;
y1 = 1;
h = 0.25;
c =-(-0.5*x1^4 + 4*x1^3 - 10*x1^2 + 8.5*x1 - y1);
x = 0:h:4;
disp(x,"x = ")
y = -0.5*x^4 + 4*x^3 - 10*x^2 + 8.5*x + c;
disp(y,"true values of y = ")
fxy = -2*x^3 + 12*x^2 - 20*x + 8.5;
y2(1) = y(1);
e(1) = (y(1) - y2(1)) * 100 / y(1);
for i = 2:17
y2(i) = y2(i-1) + fxy(i-1)*h;
e(i) = (y(i) - y2(i))*100/y(i);
end
disp(y2,"y by euler method =")
disp(e,"error =")
|