How I can eliminate an error concerning the unction FSOLVE??more details below
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Good morning to everybody. I would like first of all to apoligise both for my english and for my bad capabilities in using Matlab, but i'm quite new so even the obviousness seems to be a huge mountain to climb.
I need to make the intersection between a straight line and circle. All the data necessary are extracted from the file data_4d.mat. The letter "i", "j" ,"k" are sliding indices that allow me to select, in the file data_4d,the subject(j=1:5) the wished trial(i=1:6) and a specific time instant(k=data_4d{}{}.frame. This is the code I'm using:
load data_4d;
for j=1:5
for i=1:6
for k=data_4d{1,j}{1,i}.frame
wristx(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,10);
wristy(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,11);
wristz(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,12);
objupx(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,76);
objupy(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,77);
objupz(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,78);
objdownx(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,79);
objdowny(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,80);
objdownz(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,81);
objcenterx(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,82);
objcentery(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,83);
objcenterz(i,j,k)=data_4d{1,j}{1,i}.marker_positions(k,84);
cx(i,j,k)=(objupx(i,j,k)+objdownx(i,j,k))./2;
cy(i,j,k)=(objupy(i,j,k)+objdowny(i,j,k))./2;
cz(i,j,k)=(objupz(i,j,k)+objdownz(i,j,k))./2;
m(i,j,k)=(cz(i,j,k)-wristz(i,j,k))./(cx(i,j,k)-wristx(i,j,k));
x0 = [-20; -20]; options=optimset('Display','iter'); [x,fval] = fsolve(@myfun,x0,options) end end end
and this is the file.m myfun I need to allow the command FSOLVE to work:
function y = myfun(x,i,j,k) F = [(x(1)^2) (x(2)^2) (-2.*x(1).*cx(i,j,k)) (-2.*x(2).*cz(i,j,k)) (cx(i,j,k)^2+cz(i,j,k)^2-20^2); (-m(i,j,k).*x(1)) (x(2)) (0) (0) (-wristz(i,j,k)+m(i,j,k).*wristx(i,j,k))];
When I try to run the first code I get the following warning:
??? Input argument "i" is undefined.
Error in ==> myfun at 3 F = [(x(1)^2) (x(2)^2) (-2.*x(1).*cx(i,j,k)) (-2.*x(2).*cz(i,j,k)) (cx(i,j,k)^2+cz(i,j,k)^2-20^2);
Error in ==> fsolve at 254 fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
*How I can deal with this error?A far as I know, I should use the anonymous fucntion to pass the parameters but I don't know how to use it, even if I've read a documunt that someone on this community posted in a different thread regarding the same topic. * I would kindly thank everyboy who will help me.
Answers (1)
Alan Weiss
on 1 Aug 2012
fsolve expects its objective function to take a single vector, say x, and return a vector F(x). Your objective function myfun takes x,i,j, and k.
There are two ways you might fix this.
1. If your values of i, j, and k are fixed, then call fsolve(@(x)myfun(x,i,j,k), x0)
2. If you want fsolve to change i, j, and k, then you are out of luck, because fsolve only addresses problems with continuous values of the decision variables. If your i, j, and k could take real values, then you could try something like
function myfun(x)
i = x(2)
j = x(3)
k = x(4)
...
Alan Weiss
MATLAB mathematical toolbox documentation
1 Comment
Raffaele De Lucia
on 2 Aug 2012
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!