Newbie problem... No idea what is wrong with this, and I swear it was working earlier with exact same code :|

Apologies if this is a silly question, but I am failry new to this. Can anyone tell me how to fix this! Here's my error:
??? Attempted to access a(1); index out of bounds because numel(a)=0.
Error in ==> costv5 at 11
F=a(1)*(t^2)+a(2)*t+a(3);
Here's my code (apologies if it's messy!):
function J=costv5(EnzymeFlowrate,a1,a2,a3,a4);
global a T Y t y F e g p z
a1=e;
a2=g;
a3=p;
a4=z;
%F=a;
%F=a(1)*t+a(2);
F=a(1)*(t^2)+a(2)*t+a(3);
[t,y]=ode45('problem2v2',[0 200],[e g p z]);
d1=178;
d2=100;
d3=5;
% y specifying the matrix we are taking the rows from (number specifies
%column)
Y=[y(:,1) y(:,2) y(:,3)./10 y(:,4)];
T=t;
P=y(end,[3]);
Z=y(end,[4]);
%EnzymeFlowrate=y(end,[5]);
t=length (3);
J=-(P-(d1*(exp(-P/d2)))-(d3*Z));
end

 Accepted Answer

A simple fix is to define "a" as global in the base workspace (along with all your other globals. Better would be to read up on why you don't want all those global variables (basically because they cause problems like this).

1 Comment

In the end I didn't do this, for some reason it didn't matter whether this particular m-file worked in order for my GUI to work - so in the end I was able to just leave it, strangely!
Thanks for the answer though.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!