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 views (last 30 days)
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);

Answers (1)

Stephan
Stephan on 16 Oct 2018
Edited: Stephan on 16 Oct 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 Comments
David Akinpelu
David Akinpelu on 17 Oct 2018
Edited: David Akinpelu on 17 Oct 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-');

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!