From: James Tursa on
"Christopher " <christopher.serojano(a)veco.com.ph> wrote in message <i3tii1$nbd$1(a)fred.mathworks.com>...
>
> h = [sum(a(1:4,5)).*b(1,1).*0.25 sum(a(5:8,5)).*b(2,1).*0.25 sum(a(9:12,5)).*b(3,1).*0.25 sum(a(13:16,5)).*b(4,1).*0.25 sum(a(17:20,5)).*b(5,1).*0.25 sum(a(21:24,5)).*b(6,1).*0.25...
> sum(a(25:28,5)).*b(7,1).*0.25 sum(a(29:32,5)).*b(8,1).*0.25 sum(a(33:36,5)).*b(9,1).*0.25 sum(a(37:40,5)).*b(10,1).*0.25 sum(a(41:44,5)).*b(11,1).*0.25 sum(a(45:48,5)).*b(12,1).*0.25...
> sum(a(49:52,5)).*b(13,1).*0.25 sum(a(53:56,5)).*b(14,1).*0.25 sum(a(57:60,5)).*b(15,1).*0.25 sum(a(61:64,5)).*b(16,1).*0.25 sum(a(65:68,5)).*b(17,1).*0.25 sum(a(69:72,5)).*b(18,1).*0.25...
> sum(a(73:76,5)).*b(19,1).*0.25 sum(a(77:80,5)).*b(20,1).*0.25 sum(a(81:84,5)).*b(21,1).*0.25 sum(a(85:88,5)).*b(22,1).*0.25 sum(a(89:92,5)).*b(23,1).*0.25 sum(a(93:96,5)).*b(24,1).*0.25]';

You might consider vectorizing your code. For example, the entire h expression above can be replaced with:

h = (sum(bsxfun(@times,reshape(a(:,5),4,24),reshape(b(:,1),1,24)))*0.25).'

> f = sum(e) + x(1)*c(5,1)*0.25 + x(2)*c(5,1)*0.25 +x(3)*c(5,1)*0.25+x(4)*c(5,1)*0.25+x(5)*c(5,1)*0.25+x(6)*c(5,1)*0.25+x(7)*c(5,1)*0.25+x(8)*c(5,1)*0.25+x(9)*c(5,1)*0.25+x(10)*c(5,1)*0.25+x(11)*c(5,1)*0.25+x(12)*c(5,1)*0.25+x(13)*c(5,1)*0.25+x(14)*c(5,1)*0.25+x(15)*c(5,1)*0.25+x(16)*c(5,1)*0.25+...
> x(17)*c(5,1)*0.25

And why do you have all those multiplies? Why not factor the c(5,1)*0.25 out of the expression and multiply it by the sum of x(1) through x(17)?

James Tursa
From: Ross W on
"Christopher " <christopher.serojano(a)veco.com.ph> wrote in message <i3tii1$nbd$1(a)fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i3tiah$bh2$1(a)fred.mathworks.com>...
> > "Christopher " <christopher.serojano(a)veco.com.ph> wrote in message <i3thee$btv$1(a)fred.mathworks.com>...
> > > Hi,
> > >
> > > I am trying to use a lot of variables for my function but the optimization toolbox states that variable out of bounds because numel(x) = 2 and I can only use two variables say x(1) and x(2). . how can I increase the number of variables say using x(3),x(4),x(5),x(6)... etc.. .
> > >
> > > Can Anyone help me. . pls
> >
> > MATLAB can hold roughly 65000 variables, so I doubt you are running into that limit. We can't possibly help you unless you provide more details, and ideally some code for us to look at.
> >
> > James Tursa
>
> thank you for your reply this is my code. .
>
> function f = objfun(x)
> a = xlsread('Optimization','weekday','c8:h103'); %demand CEDC - NPC
> b = xlsread('Optimization','weekday','l8:m31'); % NPC rates
> c = xlsread('Optimization','weekday','q7:q12'); % other rates
> d = xlsread('Optimization','weekday','q13:q14');% Cemex and CPPC capacity fee
> e = a(:,2).*0.25.*c(4,1); %Cost for CEDC per 15 min
> i = 0.25.*c(5,1); %Cost for TPC per 15 min
> g = 0.25.*c(1,1); % Cost for CPPC per 15 min
> %Cost of NPC for every one hour
> h = [sum(a(1:4,5)).*b(1,1).*0.25 sum(a(5:8,5)).*b(2,1).*0.25 sum(a(9:12,5)).*b(3,1).*0.25 sum(a(13:16,5)).*b(4,1).*0.25 sum(a(17:20,5)).*b(5,1).*0.25 sum(a(21:24,5)).*b(6,1).*0.25...
> sum(a(25:28,5)).*b(7,1).*0.25 sum(a(29:32,5)).*b(8,1).*0.25 sum(a(33:36,5)).*b(9,1).*0.25 sum(a(37:40,5)).*b(10,1).*0.25 sum(a(41:44,5)).*b(11,1).*0.25 sum(a(45:48,5)).*b(12,1).*0.25...
> sum(a(49:52,5)).*b(13,1).*0.25 sum(a(53:56,5)).*b(14,1).*0.25 sum(a(57:60,5)).*b(15,1).*0.25 sum(a(61:64,5)).*b(16,1).*0.25 sum(a(65:68,5)).*b(17,1).*0.25 sum(a(69:72,5)).*b(18,1).*0.25...
> sum(a(73:76,5)).*b(19,1).*0.25 sum(a(77:80,5)).*b(20,1).*0.25 sum(a(81:84,5)).*b(21,1).*0.25 sum(a(85:88,5)).*b(22,1).*0.25 sum(a(89:92,5)).*b(23,1).*0.25 sum(a(93:96,5)).*b(24,1).*0.25]';
>
> f = sum(e) + x(1)*c(5,1)*0.25 + x(2)*c(5,1)*0.25 +x(3)*c(5,1)*0.25+x(4)*c(5,1)*0.25+x(5)*c(5,1)*0.25+x(6)*c(5,1)*0.25+x(7)*c(5,1)*0.25+x(8)*c(5,1)*0.25+x(9)*c(5,1)*0.25+x(10)*c(5,1)*0.25+x(11)*c(5,1)*0.25+x(12)*c(5,1)*0.25+x(13)*c(5,1)*0.25+x(14)*c(5,1)*0.25+x(15)*c(5,1)*0.25+x(16)*c(5,1)*0.25+...
> x(17)*c(5,1)*0.25
>
> its not yet finished. . but i tried it in the optimtool. . then an error occurs. . index out of bounds then numel(x) = 2

hi

what is the x that you pass into the function? it needs to be a vector with at least 17 elements. is it just a scalar?

how are you calling the function?

ross
From: Christopher on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i3tldu$skn$1(a)fred.mathworks.com>...
> "Christopher " <christopher.serojano(a)veco.com.ph> wrote in message <i3tii1$nbd$1(a)fred.mathworks.com>...
> >
> > h = [sum(a(1:4,5)).*b(1,1).*0.25 sum(a(5:8,5)).*b(2,1).*0.25 sum(a(9:12,5)).*b(3,1).*0.25 sum(a(13:16,5)).*b(4,1).*0.25 sum(a(17:20,5)).*b(5,1).*0.25 sum(a(21:24,5)).*b(6,1).*0.25...
> > sum(a(25:28,5)).*b(7,1).*0.25 sum(a(29:32,5)).*b(8,1).*0.25 sum(a(33:36,5)).*b(9,1).*0.25 sum(a(37:40,5)).*b(10,1).*0.25 sum(a(41:44,5)).*b(11,1).*0.25 sum(a(45:48,5)).*b(12,1).*0.25...
> > sum(a(49:52,5)).*b(13,1).*0.25 sum(a(53:56,5)).*b(14,1).*0.25 sum(a(57:60,5)).*b(15,1).*0.25 sum(a(61:64,5)).*b(16,1).*0.25 sum(a(65:68,5)).*b(17,1).*0.25 sum(a(69:72,5)).*b(18,1).*0.25...
> > sum(a(73:76,5)).*b(19,1).*0.25 sum(a(77:80,5)).*b(20,1).*0.25 sum(a(81:84,5)).*b(21,1).*0.25 sum(a(85:88,5)).*b(22,1).*0.25 sum(a(89:92,5)).*b(23,1).*0.25 sum(a(93:96,5)).*b(24,1).*0.25]';
>
> You might consider vectorizing your code. For example, the entire h expression above can be replaced with:
>
> h = (sum(bsxfun(@times,reshape(a(:,5),4,24),reshape(b(:,1),1,24)))*0.25).'
>
> > f = sum(e) + x(1)*c(5,1)*0.25 + x(2)*c(5,1)*0.25 +x(3)*c(5,1)*0.25+x(4)*c(5,1)*0.25+x(5)*c(5,1)*0.25+x(6)*c(5,1)*0.25+x(7)*c(5,1)*0.25+x(8)*c(5,1)*0.25+x(9)*c(5,1)*0.25+x(10)*c(5,1)*0.25+x(11)*c(5,1)*0.25+x(12)*c(5,1)*0.25+x(13)*c(5,1)*0.25+x(14)*c(5,1)*0.25+x(15)*c(5,1)*0.25+x(16)*c(5,1)*0.25+...
> > x(17)*c(5,1)*0.25
>
> And why do you have all those multiplies? Why not factor the c(5,1)*0.25 out of the expression and multiply it by the sum of x(1) through x(17)?
>
> James Tursa

I tried it and It has no effect with the optimtool.. the same error occured. . index out of bounds then numel(x)=2
From: Christopher on
"Ross W" <rosswoodskiwi(a)hotmail.com> wrote in message <i3tlji$b0l$1(a)fred.mathworks.com>...
> "Christopher " <christopher.serojano(a)veco.com.ph> wrote in message <i3tii1$nbd$1(a)fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i3tiah$bh2$1(a)fred.mathworks.com>...
> > > "Christopher " <christopher.serojano(a)veco.com.ph> wrote in message <i3thee$btv$1(a)fred.mathworks.com>...
> > > > Hi,
> > > >
> > > > I am trying to use a lot of variables for my function but the optimization toolbox states that variable out of bounds because numel(x) = 2 and I can only use two variables say x(1) and x(2). . how can I increase the number of variables say using x(3),x(4),x(5),x(6)... etc.. .
> > > >
> > > > Can Anyone help me. . pls
> > >
> > > MATLAB can hold roughly 65000 variables, so I doubt you are running into that limit. We can't possibly help you unless you provide more details, and ideally some code for us to look at.
> > >
> > > James Tursa
> >
> > thank you for your reply this is my code. .
> >
> > function f = objfun(x)
> > a = xlsread('Optimization','weekday','c8:h103'); %demand CEDC - NPC
> > b = xlsread('Optimization','weekday','l8:m31'); % NPC rates
> > c = xlsread('Optimization','weekday','q7:q12'); % other rates
> > d = xlsread('Optimization','weekday','q13:q14');% Cemex and CPPC capacity fee
> > e = a(:,2).*0.25.*c(4,1); %Cost for CEDC per 15 min
> > i = 0.25.*c(5,1); %Cost for TPC per 15 min
> > g = 0.25.*c(1,1); % Cost for CPPC per 15 min
> > %Cost of NPC for every one hour
> > h = [sum(a(1:4,5)).*b(1,1).*0.25 sum(a(5:8,5)).*b(2,1).*0.25 sum(a(9:12,5)).*b(3,1).*0.25 sum(a(13:16,5)).*b(4,1).*0.25 sum(a(17:20,5)).*b(5,1).*0.25 sum(a(21:24,5)).*b(6,1).*0.25...
> > sum(a(25:28,5)).*b(7,1).*0.25 sum(a(29:32,5)).*b(8,1).*0.25 sum(a(33:36,5)).*b(9,1).*0.25 sum(a(37:40,5)).*b(10,1).*0.25 sum(a(41:44,5)).*b(11,1).*0.25 sum(a(45:48,5)).*b(12,1).*0.25...
> > sum(a(49:52,5)).*b(13,1).*0.25 sum(a(53:56,5)).*b(14,1).*0.25 sum(a(57:60,5)).*b(15,1).*0.25 sum(a(61:64,5)).*b(16,1).*0.25 sum(a(65:68,5)).*b(17,1).*0.25 sum(a(69:72,5)).*b(18,1).*0.25...
> > sum(a(73:76,5)).*b(19,1).*0.25 sum(a(77:80,5)).*b(20,1).*0.25 sum(a(81:84,5)).*b(21,1).*0.25 sum(a(85:88,5)).*b(22,1).*0.25 sum(a(89:92,5)).*b(23,1).*0.25 sum(a(93:96,5)).*b(24,1).*0.25]';
> >
> > f = sum(e) + x(1)*c(5,1)*0.25 + x(2)*c(5,1)*0.25 +x(3)*c(5,1)*0.25+x(4)*c(5,1)*0.25+x(5)*c(5,1)*0.25+x(6)*c(5,1)*0.25+x(7)*c(5,1)*0.25+x(8)*c(5,1)*0.25+x(9)*c(5,1)*0.25+x(10)*c(5,1)*0.25+x(11)*c(5,1)*0.25+x(12)*c(5,1)*0.25+x(13)*c(5,1)*0.25+x(14)*c(5,1)*0.25+x(15)*c(5,1)*0.25+x(16)*c(5,1)*0.25+...
> > x(17)*c(5,1)*0.25
> >
> > its not yet finished. . but i tried it in the optimtool. . then an error occurs. . index out of bounds then numel(x) = 2
>
> hi
>
> what is the x that you pass into the function? it needs to be a vector with at least 17 elements. is it just a scalar?
>
> how are you calling the function?
>
> ross


yes x is a scalar.. but it is a variable in which i need to be evaluated to minimize the function. . how can I create a vector with seventeen variables? Is that possible?
From: us on
"Christopher "
> yes x is a scalar.. but it is a variable in which i need to be evaluated to minimize the function. . how can I create a vector with seventeen variables? Is that possible?

of course...

one of the solutions
- for three vars for sake of brevity...

v1=1*pi;
v2=2*pi;
v3=3*pi;
va=[v1,v2,v3];

us
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: MATLAB IFFT
Next: subplots with different color maps