필터 지우기
필터 지우기

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