I am pretty new to MATLAB. I am try to use ode 45 to solve a differential equation arising from a multiple source boundary condition. I want to use a for loop for the boundary condition, but I guess I am missing something

2 次查看(过去 30 天)
function Temp = proj(x,h)
b=h(1);
g=h(2);
v=20;
a=0.5;
k=45;
q=5000;
Temp(1,1)=g;
for x=0:200
if x<20
Temp(2,1)=(e^((v*x)/(2*a))*(q/k))- ((v*b)/(2*a));
else
Temp(2,1) = 0;
end
end
h0=[0;0];
xSpan=[0,200];
[xSol,hSol]=ode45(@(x,h) proj(x,h), xSpan, h0);
plot(xsol,hSol(:,1)+22);

回答(1 个)

Stephan
Stephan 2018-10-16
编辑:Stephan 2018-10-16
Hi,
maybe it is better to integrate this function stepwise, since your function is not smooth, because of the if-else statement - try:
h1=[0;0];
xSpan1=[0,20];
[xSol1,hSol1]=ode45(@(x,h) proj(x,h), xSpan1, h1);
h2=[hSol1(end,1), hSol1(end,2)];
xSpan2=[20,200];
[xSol2,hSol2]=ode45(@(x,h) proj(x,h), xSpan2, h2);
semilogy(xSol1,hSol1(:,1)+22,'r--',xSol2,hSol2(:,1)+22,'r-');
function Temp = proj(x,h)
b=h(1);
g=h(2);
v=20;
a=0.5;
k=45;
q=5000;
Temp(1,1)=g;
if x<20
Temp(2,1)=(exp((v*x)/(2*a))*(q/k))- ((v*b)/(2*a));
else
Temp(2,1) = 0;
end
end
gives:
Best regards
Stephan
  2 个评论
David Akinpelu
David Akinpelu 2018-10-17
编辑:David Akinpelu 2018-10-17
Hello, I think I am having a semantic error. the idea is to solve the ode Y" = (v*q*x)/k for all values of x between 0 and 20 and Y" = 0, for all values of x between 20 and 200. I keep getting the a constant value for all values of x in the plot. below is the code:
function Temp = proj(x,h)
b=h(1);
g=h(2);
v=20;
a=0.5;
k=45;
q=5000;
Temp(1,1)=g;
for x=0:20
Temp(2,1)= (v*q*x)/k;
end
for x=21:200
Temp(2,1) = 0;
end
end
h1=[0;0];
xSpan1=[0,20];
[xSol1,hSol1]=ode45(@(x,h) proj(x,h), xSpan1, h1);
h2=[0,0];
xSpan2=[20,200];
[xSol2,hSol2]=ode45(@(x,h) proj(x,h), xSpan2, h2);
plot(xSol1,hSol1(:,1)+22,'b--',xSol2,hSol2(:,1)+22,'r-');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by