Solving 4 non-linear equations: error using syms/subsindex
Show older comments
Hello,
I am trying to solve a set of four complicated equations for four variables on MATLAB. I have attached my code below (I hope it is decodable .. it appears to have lost format in the copy/paste process). I have been given the error '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 ':'.' Where line 766 is the first equation (EQ_1). Could someone please help me rectify this error?
Thanks, Alex.
============================================================================================================ %Defining Parameters and Variables n=3; %number of iterations
a_inc=0.3; %amplitude of incoming wave [m] T=10; %period [s] kf=[3, 4, 5]; %wave number for non-reef sections kr=[3, 4, 5]; %wave number for reef section df=0.6; %depth of water [m] dr=df-0.43; %depth of reef [m] g=9.81; %graviational constant [m/s^2] B=0.507; %width of reef [m]
l=1;
%Solving the 4 equations
syms a_i b_i c_i d_i h
EQ_1=symsum((a_i*kf(h)*g*(exp(-1i*w*t))/(w*cosh(kf(h)*df)))*int(cosh(kf(h)*(z+df))*cosh(kf(l)*(z+df)), z, 0, -df),h, 1, n)+ (a_inc*kf(1)*g*exp(-1i*w*t)/(w*cosh(kf(1)*df)))*int(cosh(kf(1)*(z+df))*cosh(kf(l)*(z+df)), z, 0, -df)-(symsum((b_i*kr(h)*g*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kf(l)*(z+df)), z, 0, -dr),h, 1, n)+ symsum((c_i*kr(h)*g*exp(1i*kr(h)*-B)*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kf(l)*(z+df)), z, 0, -dr),h, 1, n)); EQ_2=symsum((a_i*-1i*g*exp(-1i*w*t)/(w*cosh(kf(h)*df)))*int(cosh(kf(h)*(z+df))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)+ (a_inc*-1i*g*exp(-1i*w*t)/(w*cosh(kf(1)*df)))*int(cosh(kf(1)*(z+df))*cosh(kr(l)*(z+dr)))-(symsum((b_i*-1i*g*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)+ symsum((c_i*-1i*g*exp(1i*kr(h)*-B)*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)); EQ_3=symsum((b_i*kr(h)*g*exp(1i*kr*B)*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)+ symsum((c_i*kr(h)*g*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)-(symsum((d_i*kr(h)*g*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kf(h)*(z+df))*cosh(kf(l)*(z+df)), z, 0, -dr), h, 1, n)); EQ_4=symsum((b_i*-1i*g*exp(1i*kr*B)*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)+ symsum((c_i*-1i*g*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kr(h)*(z+dr))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n)-(symsum((d_i*-1i*g*exp*exp(-1i*w*t)/(w*cosh(kr(h)*dr)))*int(cosh(kf(h)*(z+df))*cosh(kr(l)*(z+dr)), z, 0, -dr), h, 1, n));
[a_i, b_i, c_i, d_i] = solve(EQ_1, EQ_2, EQ_3, EQ_4, 'a_i', 'b_i', 'c_i', 'd_i')
=============================================================================================================
Answers (1)
Walter Roberson
on 7 Jan 2017
0 votes
You cannot use a symbolic variable such as h as an index. You need to construct a a vector of definite terms and sum() those. You might possibly be able to use h as a numeric vector to calculate the terms in vectorized form, but watch out for / vs ./ and * vs .*
2 Comments
Alex Wylie
on 8 Jan 2017
Walter Roberson
on 8 Jan 2017
Your first equation a symsum from 1 to n where n is 3. If symbolic variables were permitted as indices then that would be the equivalent of evaluating the expression first with h = 1,then with 2,then with 3,and adding those. So do that. Vectorize the expression inside the symsum, use h = 1:n numeric, evaluate to get three array entries, sum() them.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!