Index in position 1 exceeds array bounds (must not exceed 1). Please help me
5 views (last 30 days)
Show older comments
Hi,
I am getting this error message on my code, Index in position 1 exceeds array bounds (must not exceed 1).
Can you check that for me
Aparently the error is on this line, M=[x1 y f(x1,y)];%Realiza la primera fila de datos iniciales
Here is the code:
function SolucionarButtonPushed2(app, event)
format short %Coloca solo 4 decimales.
x=app.DefinalaEcuacionDiferencialEditField.Value;
x1=app.LimiteinferiordelintervaloEditField.Value;%Toma el valor asignado en la interfaz para limite inferior
x2=app.LimitesuperiordelintervaloEditField.Value;%Toma el valor asignado en la interfaz para limite Superior
y=app.CondicionInicialEditField.Value;%Toma el valor de la condicion inicial
h=app.TamanodelPasoEditField.Value;%Toma el paso que se desea
f=eval(x);%Permite evaluar una funcion
mod=app.MetodoNumericoButtonGroup.SelectedObject.Text;%Me extrae el valor marcado en el Buttongroup y lo coloca como un string
%M=0;
M=[];
if strcmp(mod,"Euler")%Identifica si se seleciono el metodo de euler
M=[x1 y f(x1,y)];%Realiza la primera fila de datos iniciales
for ele=(x1+h):h:x2
dy=f((ele-h),y);%Obtiene el valor de dy/dx para el elemento ele-1
y=y+dy*h;%Obtiene un nuevo valor y
v=[ele y f(ele,y)];%Crea un nuevo vector para almacenar datos
M=[M;v];%Agrega el vector v a M
end
elseif strcmp(mod,"Heun")%Identifica si se seleciono el metodo de Heun
y0=y+f((x1-h),y)*h;%Obtiene punto medio en el intervalo utilizado
M=[x1 y ((f((x1+h),(y+f((x1),y)*h))+f(x1,y0))/2)];%Inicializa la matriz de solucion
for ele=(x1+h):h:x2 %Crea un ciclo para solucionar el metrodo
y0=y+f((ele-h),y)*h;%Obtiene punto medio en el intervalo .
dy=(f((ele-h),y)+f(ele,y0))/2;%Se obtiene el valor dy/dx
y=y+dy*h;%Obtiene un valor de y para el intervao
v=[ele y ((f((ele+h),(y+f((ele),y)*h))+f(ele,y0))/2)];%Agrega una nueva fila de solucion
M=[M;v];%es una matriz solucion
end
elseif strcmp(mod,"Punto Medio")%Identifica si se seleciono el metodo de Punto Medio
y1_2=y+f((x1),y)*h/2;%Se ovbtiene el primer valor de Y medios
M=[x1 y f((x1+h/2),y1_2)];%Se inicializa con la matriz solucion
for ele=(x1+h):h:x2%Crea un ciclo para solucionar el metrodo
y1_2=y+f((ele-h),y)*h/2;%Se ovbtiene el primer valor de Y medios
dy=f((ele-h/2),y1_2);%Se obiene el valor de dy/dx para un promedio entre los uintervalos
y=y+dy*h;%Obtiene un valor de y para el intervao
v=[ele y f((ele+h/2),(y+f((ele),y)*h/2))];%Agrega una nueva fila de solucion
M=[M;v];%es una matriz solucion
end
elseif strcmp(mod,"Runge-Kutta 4 Orden")%Identifica si se seleciono el metodo de Punto Medio
M=[x1 y f((x1),y)];%Se inicializa con la matriz solucion
for ele=(x1+h):h:x2%Crea un ciclo para solucionar el metrodo
k1=f((ele-h),y);%Parte de la ecuacion de Runge-Kutta 4° Orden
k2=f((ele-h/2),(y+1/2*k1*h));%Parte de la ecuacion de Runge-Kutta 4° Orden
k3=f((ele-h/2),(y+1/2*k2*h));%Parte de la ecuacion de Runge-Kutta 4° Orden
k4=f((ele),(y+k3*h));%Parte de la ecuacion de Runge-Kutta 4° Orden
y=y+(k1+2*k2+2*k3+k4)*h/6;%ecuacion de RK 4° orden
dy=f((ele),y);%Valor de la derivada para el intervvalo
v=[ele y dy];%Vector solucion
M=[M;v];%es una matriz solucion
end
end
app.UITable.Data=M;%Introduce matriz solucion a la tabla de la interfaz
set(app.ExportaraExcelButton,'Enable','on');%Abilita el boton de excel
Thank you
0 Comments
Answers (1)
Walter Roberson
on 24 Jan 2021
x=app.DefinalaEcuacionDiferencialEditField.Value;
x is probably going to be a character vector.
f=eval(x);%Permite evaluar una funcion
f is going to be the result of eval() of the character vector.
M=[x1 y f(x1,y)];%Realiza la primera fila de datos iniciales
f(something) is syntactically either an attempt to index an array using indices x1 and y, or else is an attempt to evaluate a function designated by f with parameters x1 and y, with there being some additional possibilities if f is a member of a matlab class such as a computer vision shape detection object.
The error message is telling you that f (the result of the eval) was not detected as being a function handle and not detected as being an object with assigned subsref method, as matlabis trying to use x1 and y as indices into whatever f is, but is finding that f does not have enough elements to satisfy the request to index.
By context in other lines we can tell that you expect f to act like a function handle. So the result of the eval(x) must be a function handle.
There are multiple ways that eval() could return a function handle. The easiest one would be if the user entered the edit field text as an anonymous function starting with @(t,x) followed by the content of their ode.
But I would suggest to you that if you are using eval() you are very likely making a mistake. If you are expecting the user to enter text in particular variable names then I suggest that you use f = str2func("@(t,x)"+x) or similar
0 Comments
See Also
Categories
Find more on Logical 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!