2017-04-21 15 views
0

Hier ist der Code ist, dass ich versuche:Numerische Antwort - Differentialgleichungen MATLAB

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) 

Wenn es eine symbolische Antwort war es funktionieren würde, aber ich denke, es ist nicht. Könnte mir jemand sagen, wie man diese Gleichung bekommt?

Dies ist die Antwort, die ich erhalten:

Warning: Explicit solution could not be found. 
> In `dsolve` (line 201) 
    uSol(x) = 
    [ empty sym ] 

+1

Für diese Art von Problem, warum verwenden Sie nicht [ 'wolfram alpha'] (http://www.wolframalpha.com/)? – qbzenker

Antwort

1

Sie können entweder ode45 oder Sie können eine numerische Methode verwenden.

Wie auch immer Sie möchten, dass Ihre Gleichung in zwei erste Ordnung Methoden

u (x) = y '(x)

u' (x) = x aufzuspalten - 0,5 * x * y + 0,5 * u/(x + 1)

y (1,3) = 0,5

U (1,3) = 2

es so selbst zu tun ...

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 [email protected](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 

Oder lassen Matlab es für Sie tun ...

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 
+0

Oh wow .. Danke! Ich bin noch nicht so fortgeschritten in der Programmierung, also verstehe ich das meiste nicht, und der Code läuft nicht so gut, also werde ich wahrscheinlich den Fehler nicht finden. – Rih

+0

Welche läuft nicht? Was ist der Fehler? – user1543042

0

In frühesten Matlab Versionen, Funktionsdefinitionen innerhalb Skripte sind nicht erlaubt. Unter die Antwort von @user1543042

Sie sollten dieses in einer Datei

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 

Und dies in einer anderen Datei:

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 
Verwandte Themen