I am trying to Optimize these set of equations to find vector c with i=1,2:
Bold variables are 3D Vectors.
Known Variables: R, o, u1, u2, l1, l2
My code works when I use fsolve but gives me an error for fminsearch
fun.m :function F = fun(x1,R,o,u1,u2,l1,l2) c=x1(1:3); kq1=x1(4); kq2=x1(5); q1=x1(6:8); q2=x1(9:11); b=x1(12:14); bnorm=x1(15:17); kc=x1(18); F(1:3) = o + kq1*(o-u1) - q1; F(4:6) = o + kq2*(o-u2) - q2; F(7) = norm(q1-c) - R; F(8) = norm(q2-c) - R; F(9) = (dot((o-q1),(q1-c))*norm(l1-q1)) - (dot((l1-q1),(q1-c))*norm(o-q1)); F(10) = (dot((o-q2),(q2-c))*norm(l2-q2)) - (dot((l2-q2),(q2-c))*norm(o-q2)); F(11:13) = (kc*bnorm)-(c-o); F(14:16) = (b/norm(b))- bnorm; F(17:19) = (cross(cross((l1-o),(u1-o)),cross((l2-o),(u2-o)))) - b; end
script.m :clear all; clc; R = 7.8; o=[0,0,0]; u1= [7.4390,7.1380,-0.0607]; u2=[6.5360,7.1380,-0.0607]; l1=[-83.7542,77.3087,-58.0508]; l2=[66.2458,74.8087,-51.0508]; x0 = ones(1,18); [x fval]= fminsearch(@fun,x0,,R,o,u1,u2,l1,l2);
Why is your error happening?
This is due to the fact that your statement returns an empty array. This means that your array does not have any entries that are not equal to . As you start going down into your code, will create an empty matrix as the number of elements in are 0 due to the call from earlier.
Next, when you get to this statement:
This loop does not execute as the number of elements in equals 0. When you get to this statement after:
will also give you an empty matrix. Finally, when you get to this statement:
You are trying to assign an empty value into a location in . Specifically, you are trying to assign one value to a location in which should be stored in , but because it's empty, this is why you are getting this error. As such, you need to double check your variable to ensure that this isn't empty before you proceed with your code.
So where is the error happening?
I can see why you are getting this error, and where this is happening. If you look towards the end of your code, you have this statement:
What you are doing is you are making such that the entries are all. When you go back into the next iteration of your loop, the statement will return an empty array as all values of are equal to . Because could not find any values that are not equal to , returns an empty array stating that it could not find any locations in that were not equal to . This is probably not what you intended to do.
Your comment states that is being set outside of the loops before your code is executed. Though that may be the case, you are changing after the first iteration of your loops, which will inevitably give you that error you are seeing.
As such, you probably need to correct this statement - either by removing it, or modifying it so that it is the definition that you are seeking.
Some tips for you
It really helps if you your code so that you can figure out what's going on. As such, MATLAB has some great facilities to help you debug your code so you can figure out why it isn't working. You need to set MATLAB to debug mode if you'd like to do this. If this is something you want, in your MATLAB editor, go to the line where you see the statement, and click on the horizontal dash beside the line number. You'll see a red dot indicating that when the function runs, the function will pause at this point.
In mode, you are able to see what the current state of execution your code is in. You can also examine what the variables look like, as well as step through line by line after this point so you can see what each line is doing. You can either type in when you're in debug mode to go to the next line, or you can do this in your MATLAB editor by clicking on the Step buttons. There are also different buttons you can choose from:
- Continue - Continue running your code until you reach the next breakpoint or the function exits
- Step - Run the next line of code
- Step In - Run the next line of code. If this line is a function, then you will go inside the function and be able to debug the statements within there. If you just do Step, it runs the function, but doesn't go inside. As such, this runs the entire function before pausing at the next line.
- Step Out - If you are inside a function, doing this will run the code until the function leaves. The code will then pause right after the point where you leave the function and you continue from there.
For more information on MATLAB debugging, check out this great MathWorks link here: http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-178