# optimization value not able to acheive

1 view (last 30 days)
Kundan Prasad on 8 Dec 2021
Commented: Kundan Prasad on 17 Dec 2021
I am not able to get the optimize value of n. Every time it shows Failure in initial objective function evaluation. FSOLVE cannot continue
Below matlab code is given. Thank you
Function
clear all
clc
function F = roo2d(n)
F=[(sin(pi*((n(1)/700)-1))/(pi*((n(1)/700)-1)))^2-0.31;
(sin(pi*((n(1)/1100)-1))/(pi*((n(1)/700)-1)))^2-0.42;
(sin(pi*((n(1)/1500)-1))/(pi*((n(1)/700)-1)))^2-0.50;
(sin(pi*((n(1)/2000)-1))/(pi*((n(1)/700)-1)))^2-0.59;
(sin(pi*((n(1)/2500)-1))/(pi*((n(1)/700)-1)))^2-0.64;
(sin(pi*((n(1)/3000)-1))/(pi*((n(1)/700)-1)))^2-0.655;
(sin(pi*((n(1)/3500)-1))/(pi*((n(1)/700)-1)))^2-0.64;
(sin(pi*((n(1)/4000)-1))/(pi*((n(1)/700)-1)))^2-0.59;
(sin(pi*((n(1)/4500)-1))/(pi*((n(1)/700)-1)))^2-0.50;
(sin(pi*((n(1)/5000)-1))/(pi*((n(1)/700)-1)))^2-0.39];
end
calling a function
fun= @roo2d
n0=[2000];
options = optimoptions('fsolve','Algorithm','levenberg-marquardt');
x= fsolve(fun,n0,options);

Walter Roberson on 9 Dec 2021
All of your equations are effectively calculations -- just with an offset for where the effective zero point is.
sinc() has a central spike that hits 1, but outside of that central spike, it never gets above for the original function -- the square of that for . So when you subtract values like 0.59, unless you are subtracting values down near 0.025 or less, then each of the functions individually has exactly two solutions, one before its central peak and one after its central peak.
With you using different divisors, you are guaranting that those central peaks do not line up -- and thus that only one function at a time has a solution. You can be certain that there is no possible solution for all of the functions simultaneously.
Walter Roberson on 15 Dec 2021
Edited: Walter Roberson on 15 Dec 2021
N=[linspace(700,5000,500) linspace(0,0.5,0.01)];
% N= linspace(700,5000,0.5)
y = cell2mat(arrayfun(@(n)roo2d(n*ones(1,4)), N, 'uniform', 0));
plot(N, y.');
yline(0, 'k')
legend(cellstr(string(1:13)), 'location', 'best')
format long g
[sol, fval] = fsolve(@roo2d, 2001*ones(1,4))
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
sol = 1×4
1.0e+00 * 3160.55329192195 1540.72536365328 0.443793615705221 0.442783020449565
fval = 13×1
-0.295527268338015 -0.164947303712957 -0.0542950346625865 -0.0936715674976596 -0.023400832165584 -0.0261077254100426 -0.0715981375285406 -0.102994370069773 -0.0910707384428546 -0.0481765550137346
function F = roo2d(n)
F=[((sin(pi*((n(1)/700)-1))/(pi*((n(1)/700)-1)))^2)*n(3)+...
((sin(pi*((n(2)/700)-1))/(pi*((n(2)/700)-1)))^2)*n(4)-0.31;
((sin(pi*((n(1)/1100)-1))/(pi*((n(1)/1100)-1)))^2)*n(3)+...
((sin(pi*((n(2)/1100)-1))/(pi*((n(2)/1100)-1)))^2)*n(4)-0.42;
((sin(pi*((n(1)/1500)-1))/(pi*((n(1)/1500)-1)))^2)*n(3)+....
((sin(pi*((n(2)/1500)-1))/(pi*((n(2)/1500)-1)))^2)*n(4)-0.50;
((sin(pi*((n(1)/2000)-1))/(pi*((n(1)/2000)-1)))^2)*n(3)+.....
((sin(pi*((n(2)/2000)-1))/(pi*((n(2)/2000)-1)))^2)*n(4)-0.59;
((sin(pi*((n(1)/2500)-1))/(pi*((n(1)/2500)-1)))^2)*n(3)+......
((sin(pi*((n(2)/2500)-1))/(pi*((n(2)/2500)-1)))^2)*n(4)-0.64;
((sin(pi*((n(1)/3000)-1))/(pi*((n(1)/3000)-1)))^2)*n(3)+.....
((sin(pi*((n(2)/3000)-1))/(pi*((n(2)/3000)-1)))^2)*n(4)-0.655;
((sin(pi*((n(1)/3500)-1))/(pi*((n(1)/3500)-1)))^2)*n(3)+....
((sin(pi*((n(2)/3500)-1))/(pi*((n(2)/3500)-1)))^2)*n(4)-0.64;
((sin(pi*((n(1)/4000)-1))/(pi*((n(1)/4000)-1)))^2)*n(3)+.....
((sin(pi*((n(2)/4000)-1))/(pi*((n(2)/4000)-1)))^2)*n(4)-0.59;
((sin(pi*((n(1)/4500)-1))/(pi*((n(1)/4500)-1)))^2)*n(3)+.....
((sin(pi*((n(2)/4500)-1))/(pi*((n(2)/4500)-1)))^2)*n(4)-0.50;
((sin(pi*((n(1)/5000)-1))/(pi*((n(1)/5000)-1)))^2)*n(3)+.....
((sin(pi*((n(2)/5000)-1))/(pi*((n(2)/5000)-1)))^2)*n(4)-0.39;
abs(n(3));
abs(n(4));
n(3)+n(4)-1];
end
Kundan Prasad on 17 Dec 2021
Thank you sir,
Now I am able achieve the optimization for more than two number of blaze wavelength

Alan Weiss on 8 Dec 2021
That is not what I get when I run your code:
No solution found.
fsolve stopped because the last step was ineffective. However, the vector of function
values is not near zero, as measured by the value of the function tolerance.
Sometiimes it is worthwhile plotting your function to see whether it might have some roots.
t = linspace(800,1e3);
z = zeros(10,length(t));
for i = 1:length(t)
z(:,i) = roo2d(t(i));
end
plot(t,z)
figure
t = linspace(1e3,3e3);
z = zeros(10,length(t));
for i = 1:length(t)
z(:,i) = roo2d(t(i));
end
plot(t,z)
function F = roo2d(n)
F=[(sin(pi*((n(1)/700)-1))/(pi*((n(1)/700)-1)))^2-0.31;
(sin(pi*((n(1)/1100)-1))/(pi*((n(1)/700)-1)))^2-0.42;
(sin(pi*((n(1)/1500)-1))/(pi*((n(1)/700)-1)))^2-0.50;
(sin(pi*((n(1)/2000)-1))/(pi*((n(1)/700)-1)))^2-0.59;
(sin(pi*((n(1)/2500)-1))/(pi*((n(1)/700)-1)))^2-0.64;
(sin(pi*((n(1)/3000)-1))/(pi*((n(1)/700)-1)))^2-0.655;
(sin(pi*((n(1)/3500)-1))/(pi*((n(1)/700)-1)))^2-0.64;
(sin(pi*((n(1)/4000)-1))/(pi*((n(1)/700)-1)))^2-0.59;
(sin(pi*((n(1)/4500)-1))/(pi*((n(1)/700)-1)))^2-0.50;
(sin(pi*((n(1)/5000)-1))/(pi*((n(1)/700)-1)))^2-0.39];
end
There is clearly no time t where all of the curves simultaneously cross 0.
Alan Weiss
MATLAB mathematical toolbox documentation
##### 3 CommentsShow 1 older commentHide 1 older comment
Kundan Prasad on 9 Dec 2021
Edited: Walter Roberson on 9 Dec 2021
I have made mistake in function value and have corrected the same. But still i am not able to get the optimized value of n as 2277
function F = roo2d(n)
F=[(sin(pi*((n(1)/700)-1))/(pi*((n(1)/700)-1)))^2-0.31;
(sin(pi*((n(1)/1100)-1))/(pi*((n(1)/1100)-1)))^2-0.42;
(sin(pi*((n(1)/1500)-1))/(pi*((n(1)/1500)-1)))^2-0.50;
(sin(pi*((n(1)/2000)-1))/(pi*((n(1)/2000)-1)))^2-0.59;
(sin(pi*((n(1)/2500)-1))/(pi*((n(1)/2500)-1)))^2-0.64;
(sin(pi*((n(1)/3000)-1))/(pi*((n(1)/3000)-1)))^2-0.655;
(sin(pi*((n(1)/3500)-1))/(pi*((n(1)/3500)-1)))^2-0.64;
(sin(pi*((n(1)/4000)-1))/(pi*((n(1)/4000)-1)))^2-0.59;
(sin(pi*((n(1)/4500)-1))/(pi*((n(1)/4500)-1)))^2-0.50;
(sin(pi*((n(1)/5000)-1))/(pi*((n(1)/5000)-1)))^2-0.39];
end
Walter Roberson on 9 Dec 2021
N = linspace(2000,2500,500);
y = cell2mat(arrayfun(@roo2d, N, 'uniform', 0));
plot(N, y.');
yline(0, 'k')
function F = roo2d(n)
F=[(sin(pi*((n(1)/700)-1))/(pi*((n(1)/700)-1)))^2-0.31;
(sin(pi*((n(1)/1100)-1))/(pi*((n(1)/1100)-1)))^2-0.42;
(sin(pi*((n(1)/1500)-1))/(pi*((n(1)/1500)-1)))^2-0.50;
(sin(pi*((n(1)/2000)-1))/(pi*((n(1)/2000)-1)))^2-0.59;
(sin(pi*((n(1)/2500)-1))/(pi*((n(1)/2500)-1)))^2-0.64;
(sin(pi*((n(1)/3000)-1))/(pi*((n(1)/3000)-1)))^2-0.655;
(sin(pi*((n(1)/3500)-1))/(pi*((n(1)/3500)-1)))^2-0.64;
(sin(pi*((n(1)/4000)-1))/(pi*((n(1)/4000)-1)))^2-0.59;
(sin(pi*((n(1)/4500)-1))/(pi*((n(1)/4500)-1)))^2-0.50;
(sin(pi*((n(1)/5000)-1))/(pi*((n(1)/5000)-1)))^2-0.39];
end
Look at the graph. Near 2250-ish, it crosses 0 for one of the functions -- but when you fsolve() you are asking for all of the functions to be solved.