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

3 visualizzazioni (ultimi 30 giorni)
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);

Risposte (1)

Stephan
Stephan il 16 Ott 2018
Modificato: Stephan il 16 Ott 2018
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 Commenti
David Akinpelu
David Akinpelu il 17 Ott 2018
Modificato: David Akinpelu il 17 Ott 2018
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-');

Accedi per commentare.

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by