Дадено е:
[tex]y = f(x)[/tex], [tex]\frac{dy}{dx} = x^{-4} + y^{2}[/tex]; и [tex]f\left(\frac{1}{\pi} \right) = -\pi[/tex];
Търси се:
[tex]f \left(\frac{4}{3\pi}\right)[/tex]
clear all
clc
% Right side of the equation
f = @(x,y) x.^(-4) + y.^2;
init = -pi;
a = 1/pi; b = 4/(3*pi);
% Numerical solutin
sol = ode45(f,[a,b],init);
xx = linspace(a,b,100);
yy = deval(sol,xx);
% Values at 4/(3*pi)
exact = 3*pi*(3*pi-4)/16;
approx = yy(end);
abs_error = abs(exact-approx);
display('Difference between the values at 4/(3*pi): ')
display(abs_error)
% Exact & numerical solution
numer = i - i * exp(2*i./xx) + ...
xx + exp(2*i./xx) .* xx;
denom = (1 + exp(2*i./xx)) .* xx.^3;
uu = -xx .* numer ./ denom;
% Absolute error of the method
err = abs(yy-uu);
display('Max. absolute error: ')
display(max(err))
% Plot the solutions
figure(1)
%%%
subplot(1,2,1)
%%%
plot(xx,yy,'b','LineWidth',3)
hold on, grid on
plot(xx,uu,'g--','LineWidth',3)
set(gca,'FontSize',14)
xlabel('\bf{x}')
ylabel('\bf{y}')
legend('\bf{Approximate}','\bf{Exact}')
axis([min(xx),max(xx),min(yy),max(yy)])
%%%
subplot(1,2,2)
%%%
plot(xx,err,'r:','LineWidth',3)
hold on, grid on
set(gca,'FontSize',14)
xlabel('\bf{x}')
ylabel('\bf{|u-y|}')
legend('\bf{Error}')
axis([min(xx),max(xx),min(err),max(err)])
Регистрирани потребители: Google Adsense [Bot], Google [Bot]