フィルターのクリア

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 Center および File ExchangeOrdinary Differential Equations についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by