Info

This question is closed. Reopen it to edit or answer.

Help: Errors in my coding

2 views (last 30 days)
Yatz Jey
Yatz Jey on 16 May 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Hey everyone, I'm getting a considerable amount of errors which I'm unsure of in my coding:-
CA0 = 0.50;
V = 1000;
v = 113;
k = 0.050;
A = [ -k - v / V, 0, 0, 0;
k, -v / V, 0, 0;
v / V, 0, -k - v / V, 0;
0, v / V, k , -v / V ];
dNdt = @(t, N, C) A * N + [ C(t) * v ; 0 ; 0 ; 0 ];
NS = fsolve(@(N) dNdt(0,N,CA0), [0; 0; 0; 0]);
Here are the respective errors:-
Attempted to access C(0); index must be a positive integer or logical.
Error in @(t,N,C)A*N+[C(t)*v;0;0;0]
Error in @(N)dNdt(0,N,CA0)
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Error in yolo (line 13)
NS = fsolve(@(N) dNdt(0,N,CA0), [0; 0; 0; 0]);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
What do these errors mean with respect to the code and how do I fix them? Any help will be appreciated thanks :)

Answers (1)

Walter Roberson
Walter Roberson on 16 May 2015
Look at your line
dNdt = @(t, N, C) A * N + [ C(t) * v ; 0 ; 0 ; 0 ];
That either says that C is a function that should be invoked with t as an argument, or it says that C is an array that should be indexed at index t. If it is an array then you are going to have difficulty if t does not happen to be a positive integer within the bounds of the array. If it is a function then we might be okay.
So let's look at the invoking call:
NS = fsolve(@(N) dNdt(0,N,CA0), [0; 0; 0; 0]);
That call passes in 0 always for the first argument, which corresponds to "t" within dNdt. 0 is not a positive integer so if C inside dNdt is an array being indexed then you are going to fail due to trying to access index 0. So we better hope that C inside dNdt was a function handle.
C inside dNdt is the third parameter in the call, so we need to look at what the third parameter was that was supplied in the call.
We can see from the fsolve() invocation that the third parameter is always CA0, so we need to look at what CA0 was defined as. We can see that it is defined as
CA0 = 0.50;
so it is not a function handle, it is a constant. Numeric constants are scalars, which are 1 x 1 arrays, so Yes, we are passing in an array in that location. And dNdt will try to index it at t and t is 0. Opps.
I cannot tell you how to fix the code because you did not happen to indicate what you hoped C(t) would mean inside the function. Perhaps you wanted C*t. But if so, with t being 0, C*t would be 0, and your formula would simplify to A*N . If that is what you are looking for, then what you are looking for is equivalent to finding a null vector for the matrix A; and as you are starting from the all-zero vector as your starting point, fsolve() would immediately find it as being the solution. Perhaps you would be wanting null() in that situation.

Community Treasure Hunt

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

Start Hunting!