Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
matlab code to find answer summation

Subject: matlab code to find answer summation

From: azira

Date: 10 Nov, 2013 08:03:06

Message: 1 of 5

hey guys, please check whether my calculation is correct or not.

question : F(x) = max {b(i) *(summation j=1 until 5 )(x(j)-a(ij))^2} ; i=1=10 ; j=1-5


 a = [ 0 0 0 0 0; 2 1 1 1 3; 1 2 1 1 2; 1 4 1 2 2; 3 2 1 0 1; 0 2 1 0 1; 1 1 1 1 1; 1 0 1 2 1; 0 0 2 1 0; 1 1 2 0 0];
 b = [ 1;5;10;2;4;3;1.7;2.5;6;3.5]
 x(1)=0; x(2)=0;x(3)=0;x(4)=0; x(5)=1;
 
sum=0;
    for j = 1:5
      for i = 1:10
         sum = sum +( (x(j)-a(i, j))^2);
      end
   end
sum

for i=1:10
    f= b(i) * sum
end

Subject: matlab code to find answer summation

From: dpb

Date: 10 Nov, 2013 16:57:47

Message: 2 of 5

On 11/10/2013 2:03 AM, azira wrote:
> hey guys, please check whether my calculation is correct or not.
...

How are we to know?

What's the desired answer and does your code produce that value or not?

--

Subject: matlab code to find answer summation

From: Marc

Date: 10 Nov, 2013 19:16:09

Message: 3 of 5

"azira" wrote in message <l5nejp$qqa$1@newscl01ah.mathworks.com>...
> hey guys, please check whether my calculation is correct or not.
>
> question : F(x) = max {b(i) *(summation j=1 until 5 )(x(j)-a(ij))^2} ; i=1=10 ; j=1-5
>
>
> a = [ 0 0 0 0 0; 2 1 1 1 3; 1 2 1 1 2; 1 4 1 2 2; 3 2 1 0 1; 0 2 1 0 1; 1 1 1 1 1; 1 0 1 2 1; 0 0 2 1 0; 1 1 2 0 0];
> b = [ 1;5;10;2;4;3;1.7;2.5;6;3.5]
> x(1)=0; x(2)=0;x(3)=0;x(4)=0; x(5)=1;
>
> sum=0;
> for j = 1:5
> for i = 1:10
> sum = sum +( (x(j)-a(i, j))^2);
> end
> end
> sum
>
> for i=1:10
> f= b(i) * sum
> end

On another note, you should not use "sum" as a dummy variable, since it is a function within Matlab.

doc sum.....

Subject: matlab code to find answer summation

From: TideMan

Date: 10 Nov, 2013 21:00:24

Message: 4 of 5

On Monday, November 11, 2013 8:16:09 AM UTC+13, Marc wrote:
> "azira" wrote in message <l5nejp$qqa$1@newscl01ah.mathworks.com>...
>
> > hey guys, please check whether my calculation is correct or not.
>
> >
>
> > question : F(x) = max {b(i) *(summation j=1 until 5 )(x(j)-a(ij))^2} ; i=1=10 ; j=1-5
>
> >
>
> >
>
> > a = [ 0 0 0 0 0; 2 1 1 1 3; 1 2 1 1 2; 1 4 1 2 2; 3 2 1 0 1; 0 2 1 0 1; 1 1 1 1 1; 1 0 1 2 1; 0 0 2 1 0; 1 1 2 0 0];
>
> > b = [ 1;5;10;2;4;3;1.7;2.5;6;3.5]
>
> > x(1)=0; x(2)=0;x(3)=0;x(4)=0; x(5)=1;
>
> >
>
> > sum=0;
>
> > for j = 1:5
>
> > for i = 1:10
>
> > sum = sum +( (x(j)-a(i, j))^2);
>
> > end
>
> > end
>
> > sum
>
> >
>
> > for i=1:10
>
> > f= b(i) * sum
>
> > end
>
>
>
> On another note, you should not use "sum" as a dummy variable, since it is a function within Matlab.
>
>
>
> doc sum.....

Also, the final loop is ineffective.
You could replace it with:
f=b(10)*sum

In addition to not using sum as a variable, you should avoid using i and j as well - by default, they are sqrt(-1).

Subject: matlab code to find answer summation

From: Roger Stafford

Date: 11 Nov, 2013 05:00:26

Message: 5 of 5

"azira" wrote in message <l5nejp$qqa$1@newscl01ah.mathworks.com>...
> question : F(x) = max {b(i) *(summation j=1 until 5 )(x(j)-a(ij))^2} ; i=1=10 ; j=1-5
> .......
- - - - - - - - -
  Assuming that your expression for F(x) is intended to mean the maximum value as i varies from 1 to 10 of b(i) times the sum from j = 1 to 5 of the term (x(j)-a(i,j))^2, then your code does not compute F(x) correctly. It should be something like (making the above assumption):

 F = -inf;
 for i = 1:10
  s = 0;
  for j = 1:5
   s = s + (x(i)-a(i,j)^2;
  end
  s = b(i)*s;
  if s > F
   F = s;
  end
 end

or you could accomplish the same thing with the one-liner:

 F = max(b.*sum(bsxfun(@minus,x(:).',a).^2,2));

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us