Argument to dynamic structure reference must evaluate to a valid field name.

5 views (last 30 days)
I was asked to use fsole to solve a problem but i keep getting this message "Argument to dynamic structure reference must evaluate to a valid field name."
this is my code
function fsolveDemo
% x(1) = theta1, x(2) = theta2
% x0 is a starting guess of theta1 and theta2.
x0 = [0.1,0.1]; % Make a starting guess at the solution
options=optimset('Display','iter'); % Option to display output
x = fsolve(@myfun,x0,options); % Call solver
plot(x(1),x(2))
end
function F = myfun(x)
t=(0:0.1:4); p1=8; p2=1+t; L1=5; L2=6;
% Rewrite the equation in the form F(x) = 0
F = [((x(1))-atan(p2./p1)+ asin(L2*sin(x(2))/sqrt.(p1.^2+p2.^2)));((x(2))- acos((p1^2+p2.^2-L1^2-L2^2)./(2*L1*L2)))];
end
please tell me whats wrong
[EDITED, Jan, please apply a proper code formatting - thanks]
  2 Comments
Jan
Jan on 17 Mar 2015
Please copy the complete error message. It is not efficient to guess, which line causes the error.
ggeneral
ggeneral on 17 Mar 2015
Argument to dynamic structure reference must evaluate to a valid field name.
Error in G_Emiko_Project2_4>myfun (line 23) F = [x(1)-atan(p2./p1)+ asin((L2*sin(x(2)))/sqrt.(p1.^2+p2.^2));
Error in fsolve (line 217) fuser = feval(funfcn{3},x,varargin{:});
Error in G_Emiko_Project2_4 (line 12) x = fsolve(@myfun,x0,options) % Call solver
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

Sign in to comment.

Answers (1)

Jan
Jan on 17 Mar 2015
Edited: Jan on 17 Mar 2015
In
sqrt.(p1.^2+p2.^2)
Matlab assumes, that "sqrt" is a struct and "(p1.^2+p2.^2)" is the field name. Remove the dot after "sqrt".
You can find the source of such problem using the debugger:
dbstop if error
Then run the code again until it stops at the error. Now evaluated the line piece by piece, until you find, which expression fails.

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!