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

2 views (last 30 days)
Hi, I am trying to solve 2-coupled equations by means of bvp4c but I get the following errors:
*??? Attempted to access x(2); index out of bounds because numel(x)=1.
Error in ==> test>blassius at 45 dxdz=[-1i*kapa*x(2)*exp(1i*delta_beta*z)
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 ==> test at 9 sol = bvp4c(@blassius,@mat4bc,solinit);*
Here is my code:
function main
clc clear all L=210e-6;
solinit = bvpinit(linspace(0,210*10^-6,100),@mat4init);
sol = bvp4c(@blassius,@mat4bc,solinit);
zint = linspace(0,L); Szint = deval(sol,zint);
plot(Szint(2,:), zint,'r','LineWidth',1)
zlabel('u/U_{\infty}', ... 'FontWeight','b','Color','w', ... 'VerticalAlignment','middle'); xlabel('x', ... 'FontWeight','b','Color','w', ... 'VerticalAlignment','bottom'); legend('u/U_{\infty} - bvp', 'u/U_{\infty}- actual') title('\bf\itBVP Example 2 - Blassius Equation','Color','w', ... 'VerticalAlignment','bottom') %set(gca,'Color','w') grid hold off %**********************************************************************
function dxdz = blassius(x,z)
lambda=1560e-9; k=2*pi/lambda; n1=2.592479; n2=2.424057; alpha=(5/4.343)*10^-7; % wat/m gp=300e-9; beta1=k*n1; beta2=k*n2; db1=beta1-1i*alpha-pi/gp; db2=beta2-1i*alpha-pi/gp; delta_beta=db1+db2; kapa=18.3*k; dxdz=[-1i*kapa*x(2)*exp(1i*delta_beta*z) 1i*kapa*x(1)*exp(-1i*delta_beta*z)];
function res = mat4bc(xa,xb)
res = [ xa(1)-1 xb(2)];
function xinit = mat4init(z)
xinit = [ z 0];
can anyone help me? Thanks in advance
  1 Comment
Jan
Jan on 8 Apr 2013
Please format your code properly. Follow the "? Help" link to learn more.
Avoid the brute clearing header "clc, clear all" because it deletes all breakpoints also. And impeding the debugging is such a bad idea, that I still wonder, why "clear all" does not create a big and fat warning in Matlab. Beside this, it removes all loaded functions from the memory, and loading them again from the hard disk is slow and wastes energy. I'm really astonished, that still teachers suggest to use this. And finally, clearing all variables at the top of a function is useless at all, because each function has its own workspace and it is empty at the beginning. Anyway, it does not solve your problem to remove it.

Sign in to comment.

Answers (1)

Jan
Jan on 8 Apr 2013
Edited: Jan on 8 Apr 2013
In you function to be integrated:
function dxdz = blassius(x,z)
x is the scalar independent variable, while z is the value of the current trajectoy and therefore a column vector. Not the other way around. So I assume, you want to swap "x" and "z" inside this function.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!