Clear Filters
Clear Filters

Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.

63 views (last 30 days)
I am getting these error messages:
Error using sym/subsindex (line 814)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic
variables, and function body must be sym expression.
Error in sym/privsubsasgn (line 1085)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 922)
C = privsubsasgn(L,R,inds{:});
Error in SolutionOfNonlinear (line 11)
eq1(v1,v2,I2) = v1.*(del_t/(2*L)) - v2.*(del_t/(2*L))+ I2 + i + v1t0.*(del_t/(2*L)) - v2t0.*(del_t/(2*L));
My code:
syms v2 v3 I2
v1 = 20;
del_t = 0.5;
R = 5000;
a= 2
i=0;
v1t0=0;
v2t0=0;
L = 0.000004;
eq1(v1,v2,I2) = v1.*(del_t/(2*L)) - v2.*(del_t/(2*L))+ I2 + i + v1t0.*(del_t/(2*L)) - v2t0.*(del_t/(2*L));
eq2(v1,v2,v3) = v1.*(del_t/(2*L)) - v2.*((1/R)-(del_t/(2*L)))+ v3.*(1/R) + i + v1t0.*(del_t/(2*L))-v2t0.*(del_t/(2*L));
eq3(v2,v3)= v2.*(1/R) - v3.*(1/R)- a.*(v2^2);
v= [0.1; 0.1; -0.1];

Accepted Answer

Askic V
Askic V on 22 Feb 2023
If you want to define equations eq1, eq2 and eq3, you need to use function handles:
syms v2 v3 I2
v1 = 20;
del_t = 0.5;
R = 5000;
a= 2
i=0;
v1t0=0;
v2t0=0;
L = 0.000004;
eq1 = @(v1,v2,I2) v1.*(del_t/(2*L)) - v2.*(del_t/(2*L))+ I2 + i + v1t0.*(del_t/(2*L)) - v2t0.*(del_t/(2*L));
eq2 = @(v1,v2,v3) v1.*(del_t/(2*L)) - v2.*((1/R)-(del_t/(2*L)))+ v3.*(1/R) + i + v1t0.*(del_t/(2*L))-v2t0.*(del_t/(2*L));
eq3 = @(v2,v3) v2.*(1/R) - v3.*(1/R)- a.*(v2^2);
v= [0.1; 0.1; -0.1];
  7 Comments
Sohaib
Sohaib on 25 Feb 2023
Thanks for your time and your suggestions. Your feedback helped me and I finally managed to run my code properly thanks to you guys. Much appreciated!
-Sohaib
Walter Roberson
Walter Roberson on 25 Feb 2023
diff() to find a derivative requires the Symbolic toolbox.
diff() can be applied to a symbolic expression. If no variable of differentiation was specified it uses symvar to pick one out
diff() can be applied to a symbolic function. If no variable of diffentiation was specified, it uses argnames to find the first parameter name and uses that
diff can be applied to an anonymous function. If no variable of differentiation was specified it uses the name of the first parameter.
diff cannot be applied to to a "simple" function handle such as @sin or @MyFunction. But if you were to use @(x,y)Myfunction(x, y) it would be fine with that.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 22 Feb 2023
syms v2 v3 I2
v1 = 20;
v1 is numeric. v2 and v3 and I2 are symbolic.
eq1(v1,v2,I2) = v1.*(del_t/(2*L)) - v2.*(del_t/(2*L))+ I2 + i + v1t0.*(del_t/(2*L)) - v2t0.*(del_t/(2*L));
v2 and I2 are still symbolic, but v1 is still symbolic.
When you have NAME(LIST) on the left side of an assignment statement in MATLAB, there are a small number of different possibilites:
  1. Every entry in LIST evaluates to non-negative integers. In this case, the values are indexing and an array assignment is being done, creating an array output;
  2. some entries in LIST might evaluate to logical() values. This is indexing as well, with each true indicating a location into which a value is to be written for an array assignment
  3. all of the entries in LIST are symbolic variables (or expressions that evaluate to symbolic variables). In this case, this is a symbolic function definition
  4. as a special case, if a single expression is used in LIST but it evaluates to a list of symbolic variables, then the result is the same as if each one in turn had been listed separately. F(x,y) = ... and F([x,y]) = ... are treated the same way, producing a symbolic function
With v1 being numeric, you must be doing an array assignment, not creating a symbolic function. But with v2 and I2 being symbolic, you cannot be doing an array assignment since v2 and I2 do not evaluate to positive integer values.
It is possible that what you want to do is
syms v1 v2 v3 I2
eq1(v1,v2,I2) = v1.*(del_t/(2*L)) - v2.*(del_t/(2*L))+ I2 + i + v1t0.*(del_t/(2*L)) - v2t0.*(del_t/(2*L));
and then later only happen to be interested in v1 = 20 -- which you would get by subs of 20 for v1 after building the symbolic results.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!