Parameter variation in BVP shooting method

11 views (last 30 days)
Hi everybody!
I wrote a code that solves the following problem:
This is a 2nd order ode and the dependant variable here is rho (tilde) and the independant variable is x (tilde). K (tilde) and B (tilde) are both parameters. The boundary conditions are:
The code is:
clear all
clc
%Shooting Method for Non-Linear ODE
k=3.25e8; %Debye length - needs to be calculated.
D=4e-9; %separation distance
y0=1; %i.c
ICguess_on_target=fzero(@(x) bar_res(x), 0.25);
[x,y]=ode45(@bar_density,[0,k.*D],[y0 ICguess_on_target]);
plot(x,y(:,1));
xlabel('\kappaD');
ylabel('$\tilde{\rho}$','Interpreter','latex')
title('$\tilde{\kappa}=\tilde{B}=1$','Interpreter','latex');
function dydx=bar_density(x,y)
kdim=1;Bdim=1;
dydx=[y(2);2.*Bdim./kdim.*y(1)+kdim.^(-1).*log(y(1))-Bdim];
end
function r=bar_res(IC_guess)
y0=1;
yf=1;
k=3.25e8;
D=4e-9;
[x,y]=ode45(@bar_density,[0,k.*D],[y0 IC_guess]);
r=y(end,1)-yf
end
This works just fine and I'm getting great results and plots of rho(tilde) as a function of x(tilde). Now I want to run the code in a loop and vary the parameters K (tilde) and B (tilde). For instance I would like to get 6 plots of rho(tilde) as a function of x(tilde) for 6 different sets of K (tilde) and B (tilde). I have been trying to insert a for loop inside the code but I'm obviously doing it wrong.
I would appreciate some help guys.
Thanks,
Roi

Accepted Answer

Alan Stevens
Alan Stevens on 29 Jul 2020
The following works:
% Shooting Method for Non-Linear ODE
k=3.25e8; %Debye length - needs to be calculated.
D=4e-9; %separation distance
y0=1; %i.c
kdimarray = 1:0.2:2; Bdimarray = 1:0.5:3.5;
ICguess_on_target = zeros(size(kdimarray));
for i = 1:6
kdim = kdimarray(i); Bdim = Bdimarray(i);
ICguess_on_target(i) = fzero(@(x) bar_res(x,kdim,Bdim), 0.25);
[x,y]=ode45(@bar_density,[0,k.*D],[y0 ICguess_on_target(i)],[], kdim, Bdim);
plot(x,y(:,1));
hold on
end
xlabel('\kappaD');
ylabel('$\tilde{\rho}$','Interpreter','latex')
title('$\tilde{\kappa}=\tilde{B}=1$','Interpreter','latex');
function dydx=bar_density(~, y, kdim, Bdim)
dydx=[y(2);2.*Bdim./kdim.*y(1)+kdim.^(-1).*log(y(1))-Bdim];
end
function r=bar_res(IC_guess,kdim,Bdim)
y0=1;
yf=1;
k=3.25e8;
D=4e-9;
[~,y]=ode45(@bar_density,[0,k.*D],[y0 IC_guess], [], kdim, Bdim);
r=y(end,1)-yf;
end
  30 Comments
Alan Stevens
Alan Stevens on 5 Nov 2020
Also, it's a good idea to insert
p1 = zeros(numel(xspan),2);
p2 = zeros(numel(xspan),2);
immediately before the
while (flag==1) && its<itmax
line.
Roi Bar-On
Roi Bar-On on 8 Feb 2021
Edited: Roi Bar-On on 8 Feb 2021
Hi Alain.
You guys helped me a lot with my previous code. I've implemented some changes and improvements to it but now having some new problems. This is still my ODE with the following bc's:
My code includes the shooting method and an intellectual guess of the derivative of rho at x=0 (transforming one BVP problems to one IVP problem), using the Implicit Euler method to write down the derivatives, solve the algebraic system with the Newton-Raphson method and make sure that I converge with infinity norm condition. I try to optimize my results by conducting bisection on the difference between a current value of rho to the its value at the bc that I chose (minimizing f1). This is the implicit Euler code:
function[x,y,f1,BISEC]=implicit_euler_method_DFT(y10,y20,x0,xf,dx,m,n)%i.g=-0.4394
x=x0:dx:xf;
y=zeros(2,length(x));
y(1,1)=y10;
y(2,1)=y20;
B=1;k=1;bc=0.25;
for i=1:1:length(x)-1
y(:,i+1)=y(:,i);
J=Jacovian(y(:,i));
g=[y(1,i+1)-dx*y(2,i+1)-y(1,i);
y(2,i+1)-dx*(2*B/k*y(1,i+1)+1/k*log(abs(y(1,i+1)))-B/k)-y(2,i)];
d=gausselemination(J,g);
f1=y(1,i+1)-bc;
BISEC=bisectionDFTbc(f1);
while (infinity_norm(d)>10^(-m))||(infinity_norm(g)>10^(-n))
y(:,i+1)=y(:,i+1)+d;
J=Jacovian(y(:,i+1));
g=[y(1,i+1)-dx*y(2,i+1)-y(1,i);
y(2,i+1)-dx*(2*B/k*y(1,i+1)+1/k*log(abs(y(1,i+1)))-B/k)-y(2,i)];
d=gausselemination(J,g);
f1=y(1,i+1)-bc;
BISEC=bisectionDFTbc(f1);
end
end
end
where x is the x tilde basically, y is rho tilde, y10 is the initial value for rho, y20 is the initial guess (-0.4394) for its derivative at x=0,x0=0, xf can be 1 or more, dx is to our desire, m and n are tolerances. The 2nd code for the bisection is:
function [x,iter_num]= bisectionDFTbc(a,b,n,m)
iter_num=0;%iteration number
x=(a+b)/2;
while sqrt((a-b)^2)>10^(-m)||sqrt(f1(x)^2)>10^(-n)
iter_num=iter_num+1;
x=(a+b)/2;
if f1(a)*f1(x)<0
b=x;
else
a=x;
end
end
x=(b+a)/2;
end
I have two main problems:
1.The y values go to infinity very fast - I don't get a stable solution.
2.Obviously I don't connect the two functions well.
I would appreciate your help.
Thanks,
Roi

Sign in to comment.

More Answers (1)

Roi Bar-On
Roi Bar-On on 29 Jul 2020
That's what I've done before. I've putted it back.
Thank you!

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!