这是我尝试过的代码:
syms y(x)
Du = diff(y,x);
ode = diff(y,x,2) - (0.5/(x+1))*diff(y,x)+0.5*x*y == x;
cond1 = y(1.3) == 0.5;
cond2 = Du(1.3) == 2;
conds = [cond1 cond2];
uSol(x) = dsolve(ode,conds)
如果有符号回答它会起作用,但我想没有。谁能告诉我如何得到这个等式的答案?
这是我得到的答案:
Warning: Explicit solution could not be found.
> In `dsolve` (line 201)
uSol(x) =
[ empty sym ]
答案 0 :(得分:1)
您可以使用ode45
或使用数值方法。
无论哪种方式,您都希望将方程式分为两个一阶方法
u(x)= y'(x)
u'(x)= x - 0.5 * x * y + 0.5 * u /(x + 1)
y(1.3)= 0.5
u(1.3)= 2
这样做自己......
function StackOverflow
close all
set(groot,'defaultLineLineWidth',3)
dx=0.01;
x = (0:dx:1.3)';
N = length(x);
Y = nan(size(x,1),2);
Y(N,:) = [0.5, 2];
F =@(x,y) [y(2) , x-0.5*x.*y(1)+0.5*y(2)./(x+1)];
for i=N:-1:2 % calculation loop
k_1 = F(x(i), Y(i, :));
k_2 = F(x(i) + 0.5*dx, Y(i,:) + 0.5*dx*k_1);
k_3 = F(x(i) + 0.5*dx, Y(i,:) + 0.5*dx*k_2);
k_4 = F(x(i) + dx, Y(i,:) + dx*k_3);
Y(i-1,:) = Y(i,:) + (dx/6)*(k_1+2*k_2+2*k_3+k_4); % main equation
end
y = Y(:,1);
u = Y(:,2);
figure(1)
set(gcf, 'units', 'normalized')
subplot(2,1,1)
plot(x , y)
set(gca,'fontsize',14, 'Position',[0.12 0.57 0.85 0.40])
xlim([min(x), max(x)])
xticks(linspace(min(x), max(x), 5))
xticklabels({})
ylabel('$y(x)$', 'Interpreter', 'latex')
subplot(2,1,2)
plot(x , u)
set(gca,'fontsize',14, 'Position',[0.12 0.125 0.85 0.40])
xlim([min(x), max(x)])
xticks(linspace(min(x), max(x), 5))
xlabel('$x$', 'Interpreter', 'latex')
ylabel('$y''(x)$', 'Interpreter', 'latex')
end
或者让Matlab为你做这件事......
function StackOverflow2
%Note: t = xMax - x
[t,Y] = ode45(@F ,[0 1.3],[0.5; 2]);
z = flipud([t,Y]);
x = max(t) - z(:,1);
y = z(:,2);
u = z(:,3);
figure(2)
subplot(2,1,1)
plot(x , y)
set(gca,'fontsize',14, 'Position',[0.12 0.57 0.85 0.40])
xlim([min(x), max(x)])
xticks(linspace(min(x), max(x), 5))
xticklabels({})
ylabel('$y(x)$', 'Interpreter', 'latex')
subplot(2,1,2)
plot(x , u)
set(gca,'fontsize',14, 'Position',[0.12 0.125 0.85 0.40])
xlim([min(x), max(x)])
xticks(linspace(min(x), max(x), 5))
end
function dydt = F(t,y)
dydt = [y(2); (1.3-t)-0.5*(1.3-t).*y(1)+0.5*y(2)./((1.3-t)+1)];
end
答案 1 :(得分:0)
在最早的Matlab版本中,脚本中不允许使用函数定义。从@user1543042
得到答案你应该把它放在一个文件中
function StackOverflow2
%Note: t = xMax - x
[t,Y] = ode45(@F ,[0 1.3],[0.5; 2]);
z = flipud([t,Y]);
x = max(t) - z(:,1);
y = z(:,2);
u = z(:,3);
figure(2)
subplot(2,1,1)
plot(x , y)
set(gca,'fontsize',14, 'Position',[0.12 0.57 0.85 0.40])
xlim([min(x), max(x)])
xticks(linspace(min(x), max(x), 5))
xticklabels({})
ylabel('$y(x)$', 'Interpreter', 'latex')
subplot(2,1,2)
plot(x , u)
set(gca,'fontsize',14, 'Position',[0.12 0.125 0.85 0.40])
xlim([min(x), max(x)])
xticks(linspace(min(x), max(x), 5))
end
这是另一个文件:
function dydt = F(t,y)
dydt = [y(2); (1.3-t)-0.5*(1.3-t).*y(1)+0.5*y(2)./((1.3-t)+1)];
end