Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem with my assgnment in solving two second differentiation equation with certain boundary conditions

Subject: Problem with my assgnment in solving two second differentiation equation with certain boundary conditions

From: jieqiong liu

Date: 3 Oct, 2010 15:33:04

Message: 1 of 2

I am trying to work out with my assignment in solving two second order differentiation equation with bounday conditions when z = 0, C10= 1.2, C20= 0;z=L=0.4, C1L=0, C2L=0.8. So I type with the following code:


function stagnantbvp_testing

solinit = bvpinit([0:0.01:0.4],[1.2 0]);
sol=bvp4c(@stagnant_ode,@stagnant_bc,solinit);

zint=[0:0.01:0.4];
xint=deval(sol,zint);

plot(zint,xint(1,:))
%axis([0 0.4]);
title('Concentration Profile');
hold
plot(zint,xint(1,:),'r')
xlabel('surface distance')
ylable('concentrations')

%----------------------------------------------------------------------
function dxdz = stagnant_ode(x,z)

% since the states are passed in as a single vector,let
% x(1)=C(1)
% x(3)=C(2)

%dxdz=[x(2);((3.33e-3)/(1.99e-5))*x(1)*x(3);x(4);((3.33e-3)/(4.55e-6))*x(1)*x(3)];
dxdz(1) = x(2);
%dC(1)dz = x(2)

dxdz(2) = ((3.33e-3)/(1.99e-5))*x(1)*x(3);
%dC(1)dz = ((3.33e-3)/(1.99e-5))*C(1)*C(2)

dxdz(3) = x(4);
%dC(2)dz=x(4)

dxdz(4) = ((3.33e-3)/(4.55e-6))*x(1)*x(3);
%dC(2)dz=((3.33e-3)/(4.55e-6))*C(1)*C(2);

dxdz = dxdz(:);
% This ensures that the vector returned is a column vector

%-----------------------------------------------------------------------
function res = stagnant_bc(xa, xb)

 %xa = [1.2 0 0 0];
 %xb = [0 0 0.8 0];
 
 res=[xa(2)-1.2
      xb(2)
      xa(1)
      xb(1)-0.8];

%-----------------------------------------------------------------------

However, they often displayed the same error message I don't really understand,

??? Attempted to access x(2); index out of bounds because numel(x)=1.

Error in ==> stagnantbvp_testing>stagnant_ode at 25
dxdz(1) = x(2);

Error in ==> bvparguments at 116
    testODE = ode(x1,y1,odeExtras{:});

Error in ==> bvp4c at 130
[n,npar,nregions,atol,rtol,Nmax,xyVectorized,printstats] = ...

Error in ==> stagnantbvp_testing at 4
sol=bvp4c(@stagnant_ode,@stagnant_bc,solinit);


Can any one help me please? This is really urgent~

Subject: Problem with my assgnment in solving two second differentiation

From: Torsten Hennig

Date: 4 Oct, 2010 07:39:06

Message: 2 of 2

> I am trying to work out with my assignment in solving
> two second order differentiation equation with
> bounday conditions when z = 0, C10= 1.2, C20=
> 0;z=L=0.4, C1L=0, C2L=0.8. So I type with the
> following code:
>
>
> function stagnantbvp_testing
>
> solinit = bvpinit([0:0.01:0.4],[1.2 0]);
> sol=bvp4c(@stagnant_ode,@stagnant_bc,solinit);
>
> zint=[0:0.01:0.4];
> xint=deval(sol,zint);
>
> plot(zint,xint(1,:))
> %axis([0 0.4]);
> title('Concentration Profile');
> hold
> plot(zint,xint(1,:),'r')
> xlabel('surface distance')
> ylable('concentrations')
>
> %-----------------------------------------------------
> -----------------
> function dxdz = stagnant_ode(x,z)
>
> % since the states are passed in as a single
> vector,let
> % x(1)=C(1)
> % x(3)=C(2)
>
> %dxdz=[x(2);((3.33e-3)/(1.99e-5))*x(1)*x(3);x(4);((3.3
> 3e-3)/(4.55e-6))*x(1)*x(3)];
> dxdz(1) = x(2);
> %dC(1)dz = x(2)
>
> dxdz(2) = ((3.33e-3)/(1.99e-5))*x(1)*x(3);
> %dC(1)dz = ((3.33e-3)/(1.99e-5))*C(1)*C(2)
>
> dxdz(3) = x(4);
> %dC(2)dz=x(4)
>
> dxdz(4) = ((3.33e-3)/(4.55e-6))*x(1)*x(3);
> %dC(2)dz=((3.33e-3)/(4.55e-6))*C(1)*C(2);
>
> dxdz = dxdz(:);
> % This ensures that the vector returned is a column
> vector
>
> %-----------------------------------------------------
> ------------------
> function res = stagnant_bc(xa, xb)
>
> %xa = [1.2 0 0 0];
> %xb = [0 0 0.8 0];
>
> res=[xa(2)-1.2
> xb(2)
> xa(1)
> xb(1)-0.8];
>
> %-----------------------------------------------------
> ------------------
>
> However, they often displayed the same error message
> I don't really understand,
>
> ??? Attempted to access x(2); index out of bounds
> because numel(x)=1.
>
> Error in ==> stagnantbvp_testing>stagnant_ode at 25
> dxdz(1) = x(2);
>
> Error in ==> bvparguments at 116
> testODE = ode(x1,y1,odeExtras{:});
>
> Error in ==> bvp4c at 130
> [n,npar,nregions,atol,rtol,Nmax,xyVectorized,printstat
> s] = ...
>
> Error in ==> stagnantbvp_testing at 4
> sol=bvp4c(@stagnant_ode,@stagnant_bc,solinit);
>
>
> Can any one help me please? This is really urgent~


You initialize bvp4c for _two_ unknown
functions, but you have _four_ of them.

Best wishes
Torsten.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us