Descent direction error using fminunc in matlab - matlab

I am optimizing a kernel in matlab, but on one of the loops it outputs the following error:
Error using lineSearch Search direction is not a descent direction;
roundoff errors may be affecting convergence.
and exits the whole program. I'm actually fine with there being an error, I just would want it to continue to the next loop rather than exiting the program and making me start all over, only to run into the same error again. Is there a way to just make it just return an NAN or whatever number it managed to hit upon and move on to the next iteration?
Edit:
I have reproduced the error with the following lines of code
options = optimset('Display','off','TolFun',4e-16,'LargeScale','off');
par12=[4.45873897125075124848,2.45448132035209054536,398.23544583453281120455];
psfY12=[332.20478974188495158160,105.81515908458436570072,0,217.29169454128577854135,47.84122489713877257600,398.23544583453281120455,186.32064258820469149214,69.24275387165039319370,33.13754887984555352887];
y12=1:9;
fp1D = fminunc(#errfun,par12,options,psfY12,y12);
function [z] = errfun(p,v,x);
%cx = p(1);
%wx = p(2);
%amp = p(3);
zx = p(3)*exp(-0.5*(x-p(1)).^2./(p(2)^2)) - v;
z = sum(zx.^2);
end
Also I traced the error to the following line:
if strcmpi(output.algorithm, medium)
[x,FVAL,GRAD,HESSIAN,EXITFLAG,OUTPUT] = fminusub(funfcn,x, ...
options,defaultopt,f,GRAD,sizes,flags,finDiffFlags,varargin{:});
But I doing
open fminusub
returns the error:
Error using open (line 86)
File 'fminusub' not found.

fminusub is a private function to fminunc.
One idea is to add 'Display','Iter' to your optimoptions call, so that you can see more details of what the optimizer is doing. It is possible that the optimizer has gotten into a poorly conditioned region, or is close to a local minimum or a saddle point, causing the finite difference approximation of the gradient of your cost function to behave poorly.
One suggestion is to try setting FiniteDifferenceStepSize to some small value in the optimoptions call.
Here's an example:
optimoptions(#fminunc, 'MaxIterations', 10000, 'MaxFunctionEvaluations', 50000, 'Display', 'Iter', 'FiniteDifferenceStepSize', 1e-3);

Related

Unexpected length of array and plotting error as a result

I wanted to plot the load voltage across the resistor in series with a diode using matlab. This is a rather simple example involving piecewise functions, however I ran into an unexpected error.
t=[0:0.001:10]
vs=4*sin(pi * t)
for i =1:length(vs)
if(vs(i)<=0.7)
v(i)=0;
else
v(i)=vs(i)-0.7;
end
end
plot(t,v)
t is the time, vs is the source voltage, and v is the load voltage. Now, running this gave me an error saying "error, t and v are of different sizes..". Using length() I found out that while t and vs are of lengths 10001, v is somehow of length 1000001.
This is truly baffling to me. How can v and vs possibly differ in size? every element of vs was mapped to an element of v, and yet the size of v comes out to be about 100 times the size of vs.
Being new to matlab, I still am not very comfortable with not explicitly declaring the array v before using it in the for loop. However, I went through with it, because the example I worked on prior to this, used the same thing and it worked without any problems. We simply had a plot a piecewise function:
x=[-2 : 0.00001 : 20];
for i=1: length(x)
if(x(i)>=-2 && x(i)<0)
y(i)=sqrt(x(i)^2+1);
else if(x(i)>=0 && x(i)<10)
y(i)=3*x(i)+1;
else
y(i)=9*sin(5*x(i)-50);
end
end
end
plot(x,y)
This code worked flawlessly, and in my opinion the initial code is fundamentally doing the same thing, so again, I'm clueless as to why it failed.
The original code works if you initialise v to be of the same size as t (and therefore, vs), but still, I want to know why the code involving x,y worked (where y wasn't initialised) and the code involving (t,v) failed.
Also, my friend copy pasted the entire code into the command window of matlab 2016, and it worked. (and I'm using the 2021 version).
Its good practice to initialize variables before entering a loop. It will help avoid undefinied behaviour when you run the script multiple times. If you run the script with different lengths for t, it would fail the second run. One solution would be:
t=0:0.001:10;
vs=4*sin(pi * t);
v=nan(size(t));
for i =1:length(vs)
if(vs(i)<=0.7)
v(i)=0;
else
v(i)=vs(i)-0.7;
end
end
figure;
plot(t,v);
You could also avoid the for loop and use matrix operations instead:
t=0:0.001:10;
vs=4*sin(pi * t);
v=vs-0.7;
v(vs<=0.7)=0;
figure;
plot(t,v);

Matlab can't see a specific function when running in parallel

This has got me stumped.
I've written a function parObjectiveFunction that runs several simulations in parallel using createJob and createTask. It takes as an argument an objectiveFunction which is passed deeper into the code to calculate the objective function value for each simulation.
When I run parObjectiveFunction from the directory where objectiveFunction is found, it works as expected, but when I go one level up, it can no longer find objectiveFunction. The specific error I get is
Error using parallel.Job/fetchOutputs (line 1255)
An error occurred during execution of Task with ID 1.
Error in parObjectiveFunction (line 35)
taskoutput = fetchOutputs(job);
Caused by:
Error using behaviourObjective/getPenalty (line 57)
Undefined function 'objectiveFunction' for input arguments of type 'double'.
(behaviourObjective is an object)
This is weird for several reasons.
objectiveFunction is definitely in path, and when I try which objectiveFunction, it points to the correct function.
I have other components of the deeper code in other directories, and they are found without issue (they are objects rather than functions, but that shouldn't make a difference).
There's a line of code in parObjectiveFunction that runs the simulation, and when I run that directly in the matlab command window it finds objectiveFunction without issue.
I get the same results on my local machine and an HPC server.
My first thought was that the individual task might have its own path which didn't include objectiveFunction, but then that should cause problems for the other components (it doesn't). The problem is compounded because I can't work out how to debug the parallel code.
What am I doing wrong? Code that produced the issue is below.
Are there any known issues where matlab can't find functions when
using parallel processing with createJob, createTask, submit
and fetchOutputs?.
How can you debug in matlab when the issue is
only when operating in parallel? None of my print statements appear.
To make something work for external testing would take quite a bit of hacking, but for the sake of the question, the parallel function is:
function penalty = parObjectiveFunction(params, objectiveFunction, N)
% Takes a vector of input parameters, runs N instances of them in parallel
% then assesses the output through the objectiveFunction
n = params(1);
np = params(2);
ees = params(3);
ms = params(4);
cct = params(5);
wt = params(6);
vf = params(7);
dt = 0.001;
bt = 10;
t = 10;
c = parcluster;
job = createJob(c);
testFunction = #(run_number)behaviourObjective(objectiveFunction,n,np,ees,ms,cct,wt,vf,t,dt,bt,run_number);
for i = 1:N
createTask(job, testFunction, 1, {i});
end
submit(job);
wait(job);
taskoutput = fetchOutputs(job);
pensum = 0;
for i = 1:N
pensum = pensum + taskoutput{i}.penalty;
end
penalty = pensum/N;
end
It sounds like you need to attach some additional files to your job. You can see which files were picked up by MATLAB's dependency analysis by running listAutoAttachedFiles, i.e.
job.listAutoAttachedFiles()
If this isn't showing your objectiveFunction, then you can manually attach this by modifying the AttachedFiles property of the job.
It seems as though objectiveFunction is a function_handle though, so you might need to something like this:
f = functions(objectiveFunction)
job.AttachedFiles = {f.file}

While loop error with Netwon-Raphson Method

I am trying to find use to Newton-Raphson method to find the roots. It does this by making a guess and then improving the guess after each iteration until you get one of the zeros.
Because the Newton-Raphson method quickly finds the zeros, it gives me a small error immediately and after two or three iterations max it should fail to meet the conditions of the while loop. However, the problem is that when I remove the semi-colon after "error" in my loop, I start getting fractions that should break the while loop, but its like Matlab doesn't know that 123/8328423 is less than 1. It continues to run until I manually force the program to stop running.
How do I fix this? I tried format long, format longe, and using double both in the command window, in the scrip file, and somewhere in the loop.
Thank you in advance for any tips, suggestions, or advice that may help!!
A = [1,2,-4;2,-2,-2;-4,-2,1;];
format longe
% syms x y z
% P = x^4 + 3*x^2*y^2-z^3+y+1;
% feval(symengine,'degree',P,x)
syms x
B = mateigenvalue(A);
f(x) = simplify(matdet(B));
x0 = 1;
error = 10;
while(error > .01)
x1 = x0 - f(x0)/(27*(x0)-3*(x0)^2);
error = abs(((f(x0)-f(x1))/f(x0))*100)
x0 = x1;
end
x0 = double(x0)
I reckon the main problem is with error.
It starts as double but inside the while-loop it turns into a symbolic variable and you can't easily compare symbolic variables with scalar values (the .01 in the while-loop condition).
Check in your workspace if error is symbolic (or type class(error) and check if sym is returned). I guess it is symbolic because a fraction is returned (123/8328423), as instead Matlab treats double values with decimals, not fractions.
If so, try doing (inside the while-loop) a conversion for error that is, under the line
error = abs(((f(x0)-f(x1))/f(x0))*100);
try putting
error=double(error);
So error will be temporarily converted in double and you can easily compare its value with .01 to check the while-loop condition.
Also, it is bad practice to call a variable error since error() is a built-in function in Matlab. By naming a variable error you cannot use the error() function. Same story goes for other built-in functions.

Using a Jacobian with MatCont

I am trying to solve a very large non-linear problem using the MatCont package. Due to the large number of dimensions, and the non-linear nature, I believe that supplying the Jacobian for my system to the MatCont algorithm will speed things up immensely. However, I cannot get it to recognise that it has a Jacobian to use!
As a minimal working example, I have modified the circle finder from the help documentation to include a Jacobian:
function out = curve()
out{1}=#curvefunc;
out{2}=#defaultprocessor;
out{3}=#options;
out{4}=#jacobian;
out{13}=#adapt;
end
function f=curvefunc(x)
f=x(1)^2+x(2)^2-1;
end
function J=jacobian(x)
disp('USE JACOBIAN')
J=[2*x(1) , 2*x(2)];
end
function varargout=defaultprocessor(varargin)
if nargin>2
varargout{3}=varargin{3};
end
varargout{2}=[0];
varargout{1}=0;
end
function option=options()
option=contset;
end
function [res,x,v]=adapt(x,v)
res=[];
end
I then try to run this program from the command line using
[x,v,s,h,f] = cont(#curve,[1;0]);
However, the response is
first point found
tangent vector to first point found
elapsed time = 0.2 secs
npoints curve = 300
Since I told it to output 'USE JACOBIAN' every time the Jacobian function was called, it is clear that MatCont is not using it.
How do I get the Jacobian to be used?
I solved my own problem! Seems I was quite close to getting it working. The below is a bit of a botch, so if anyone knows how to do it with options please post an answer also.
Firstly I edit the options settings so that when it performs the continuation it only locates the first point:
function option=options()
option = contset;
option = contset(option,'MaxNumPoints',1);
end
its fine for it to do this using numerical Jacobians, the first point is known very well in most problems. This is then called from a script or a function using the following:
[x,v,s,h,f] = cont(#curve,[1;0]);
global cds
cds.options.MaxNumPoints=[];
cds.symjac=1;
[x,v,s,h,f] = cont(x,v,s,h,f,cds);
The first line finds the initial point using numerical Jacobians, as it was set up to do. The continuer is then manually adjusted to firstly have no limit on the maximal number of points (this can be set to any appropriate number) and then the use of the user provided Jacobian is set to 1 (true). The continuer is then resumed with the new settings and uses the Jacobain properly.

About using quad in MATLAB

I encounter a strange problem with quad function.
I was using quad to calculate simple integral, and it worked for 10 to 20 times, then Matlab issues the following error:
Error using quad (line 75)
The integrand function must return an output vector of the same length as the input vector.
yteor(k) = quad(#(q)(exp(-(q).^2).*q.^2/(k.^2+1)), 0, 1);
Here q and k are scalars.
I can not get what is wrong and why it worked several hours ago.
Edit
Here is my code
for k=1:100,
xteor(k)=step*k;
yteor(k)=quad(#(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1);
end plot(xteor,yteor,'r');
The following snippet works for me on Octave (Matlab GNU clone)
step = 1;
xteor = zeros(100,1);
yteor = zeros(100,1);
for k=1:100,
xteor(k)=step*k;
yteor(k)=quad(#(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1);
end
plot(xteor,yteor,'r');
pause
My hypothesis is that your error is the consequence of something else happening earlier in your code (maybe related to step not being a scalar?). Instead of focusing on this line where the error arise. Try to search what you have changed just before the error appear.