optimization of n for dual blaze wavelength

7 次查看(过去 30 天)
This code has two optimization value along with two weight i.e. [1541;3160;0.5;0.5]
everytime i run the code i recieved the mentioned error
Please provide the solution of same.
thank you
clear all
clc
% N=[linspace(700,5000,0.5) linspace(0,0.5,0.01)];
N= linspace(700,5000,500);
% n= linspace(0,0.5,500);
y = cell2mat(arrayfun(@roo2d, N, 'uniform', 0));
% Z = cell2mat(arrayfun(@roo2d, X, 'uniform', 0));
plot(N, y.');
yline(0, 'k')
format long g
[sol, fval1] = fsolve(@roo2d, 1300);
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
I am receiving this error everytime
Index exceeds the number of array elements (1).
Error in multiroo2d>roo2d (line 13)
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;
Error in multiroo2d (line 6)
y = cell2mat(arrayfun(@roo2d, N, 'uniform', 0));

回答(1 个)

Rushil
Rushil 2025-5-6
Hello
After reviewing the provided code, the function “roo2d” expects an input vector of length 4, as it references elements through within its definition. However, in the following line of code,
y = cell2mat(arrayfun(@roo2d, N, 'uniform', 0));
The variable N is a vector with 500 elements, and the expression “arrayfun(@roo2d, N, ...)” applies the “roo2d” function to each individual value in N, passing a single number (such as 700, 737, etc.) as the argument n.
As a result, within the “roo2d” function, n is a scalar rather than a vector of length 4. Thus, when MATLAB tries to access , , or , it produces an "Index exceeds array bounds" error.
To resolve the issue, one possible workaround is by changing variable , keeping the others fixed.
Refer to the below given code snippet for a better understanding:
N = linspace(700,5000,500);
n2 = 1000; % choose appropriate values
n3 = 0.5;
n4 = 0.5;
y = cell2mat(arrayfun(@(n1) roo2d([n1, n2, n3, n4]), N, 'UniformOutput', 0));
plot(N, y.');
yline(0, 'k');
Hope it helps

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by