optimization value not able to acheive

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);

 Accepted Answer

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.

8 Comments

I have attached the pdf of what i want to achieve. Moreover I have attached the screenshot of it. The paper i am trying to replicate is given with hyperlink. I want to optimize the code for single blaze wavelenght, followed by the multi-blaze wavelenght.
Please provide the solution of same. Thank you
Read section E2 and F1 ! It says that you cannot expect the system to have a zero, and that instead you are just trying to find minimum error !
The problem that you had was that your initial point was exactly where one of the components is numerically undefined. You divide by (pi * (n(1)/2000-1)) but that is pi * 0 when n(1) = 2000 exactly, and that gives you the NaN.
format long g
[sol, fval] = fsolve(@roo2d, 2001)
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
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.
sol =
2277.41645109099
fval = 10×1
-0.299807234490496 -0.415746780114875 -0.124039174417467 0.34828407762878 0.334191903268672 0.168131040891419 0.0177767363362461 -0.0691924368700483 -0.0848070434951021 -0.0549144920748943
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
Thank you sir, I got it but i need further help for remaning section.
Sir i am facing problem for optimization of dual blaze wavelenght.
the code is given below
N=[linspace(700,5000,500) linspace(0,0.5,0.01)];
% N= linspace(700,5000,0.5)
y = cell2mat(arrayfun(@roo2d, N, 'uniform', 0));
plot(N, y.');
yline(0, 'k')
format long g
[sol, fval] = fsolve(@roo2d, 2001)
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
abs(n(3));
abs(n(4));
Those want n(3) and n(4) to be 0 .
n(3)+n(4)-1
That wants n(3)+n(4) to be 1, even though they are individually 0. This is a contradiction that can only be resolved by remembering that sometimes the purpose of running fsolve() is not to find a root but rather to find a best-fit.
abs(n(4));
That cannot work unless n is a vector of length 4 or greater. How is the function being called?
y = cell2mat(arrayfun(@roo2d, N, 'uniform', 0));
That is going to invoke roo2d passing in each element of N in turn -- which means passing in a scalar each time, not a vector of length 4 or more.
[sol, fval] = fsolve(@roo2d, 2001)
That would pass in a the scalar value 2001 to roo2d, which is not a vector of length 4 or more.
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
Thank you sir,
Now I am able achieve the optimization for more than two number of blaze wavelength

Sign in to comment.

More Answers (1)

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 Comments

Actual i am not getting it. The optimize value of n should be 2277
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
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.

Sign in to comment.

Categories

Find more on Signal Processing Toolbox 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!