Error in defining variable/equation

I have the following code:
----------------------------------------
%defining variables:
clear; warning('off','MATLAB:ezplot:NotVectorized');
x(1) = 1.1; %Upper spring position
x(2) = 0.8; %Lower spring position
x(3) = 0.95; %Control arm length
x(4) = 0.6; %0g angle in radians (Theta_0)
x(5) = 90e3; %spring constant (k)
alphamax = 0.6*x(4); %max rotation for plotting
syms a1; %angle of deflection from Theta_0
%equation 1: Vertical tire/shaft deflection as a function of alpha
eq1(a1,x) = x(3)*sin(x(4)) - x(3)*sin(x(4) - a1);
figure(1);clf
ezplot(@(a1) eq1(a1,x),[0,alphamax])
ylabel('vertical deflection of tire/shaft')
------------------------------------------------------------------------------
When I run it, I get the following errors:
------------------------------------------------------------------
Error using sym/subsindex (line 766)
Invalid indexing or function definition. When defining a function, ensure that the arguments are
symbolic variables and the body of the function is a SYM expression. When indexing, the input must be
numeric, logical, or ':'.
Error in sym/privsubsasgn (line 1031)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 868)
C = privsubsasgn(L,R,inds{:});
Error in Project_deliverable1 (line 14)
eq1(a1,x) = x(3)*sin(x(4)) - x(3)*sin(x(4) - a1);
-----------------------------------------------------------------
I have tried different ways of defining a1, but have not had any luck. Can anybody shed some light?
Thank you very much.

Réponses (1)

Walter Roberson
Walter Roberson le 2 Oct 2016
You define numeric array x, and then you define
syms a1; %angle of deflection from Theta_0
eq1(a1,x) = x(3)*sin(x(4)) - x(3)*sin(x(4) - a1);
The right hand side of the assignment is symbolic because of the presence of the symbolic variable a1
On the left hand side, you have the undefined eq1 and you have (a1,x) after that. The a1 is symbolic, but the x is numeric.
If both parts of the (a1,x) were symbolic, like
syms a1 y
eq1(a1, y) = x(3)*sin(x(4)) - x(3)*sin(x(4) - a1);
then MATLAB would understand that you wanted to create a symbolic function whose formula was x(3)*sin(x(4)) - x(3)*sin(x(4) - a1) .
If both parts of the (a1,x) were numeric, like
syms a1
a2 = 9
eq1(a2,x) = x(3)*sin(x(4)) - x(3)*sin(x(4) - a1);
then MATLAB would understand the expression as indicating that you wanted to assign the (symbolic) value x(3)*sin(x(4)) - x(3)*sin(x(4) - a1) to the locations eq1(a2,x(1)), eq1(a2,x(2)), eq1(a2,x(3)), eq1(a2,x(4)), and eq1(a2,x(5)) . That assignment would fail because none of those x(1) through x(5) are valid indices for an array, but if they all just happened to be positive integers then the assignment would work.
But as it is, with the undefined eq1 and the symbolic a1 and the numeric x, MATLAB does not know what to make of attempting to assign to eq1(a1,x) and so gives you the error message.
You need to decide whether you are assigning a value, or if you are assigning a formula, or if you are assigning a function handle . If you are assigning a value, the left side of the expression cannot have any symbolic variables as subscripts. If you are assigning a formula, the left side of the expression cannot have any numeric subscripts, only symbolic ones. If you are assigning a function handle, the left side cannot have any () subscripts at all in the final component (but could have {} subscripts.)
I would suggest to you that what you want is:
eq1 = @(a1) x(3)*sin(x(4)) - x(3)*sin(x(4) - a1);
figure(1);clf
ezplot( eq1, [0,alphamax])
with no "syms a1". This would create eq1 as a function handle with a single parameter named a1 in the body of the anonymous function.

Community Treasure Hunt

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

Start Hunting!

Translated by