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:
Fucntion with loops

Subject: Fucntion with loops

From: Benp P

Date: 15 Dec, 2008 06:39:02

Message: 1 of 6

Hi,,

I am trying to write a function takes a vector a as its input, and returns a scalar obtained by summing a series of products of the elements of x such as

 function f = vsquare(x)
global L;
global Weights;
global n;
global LogVols;

term = 0;
E = zeros(1,n*(n+1)/2);
for i = 1:n
    for j = 1:i
        term = term + 1;
        E(term) = (L(n-i+1,n-j+1)*x(n-j+1)*Weights(n-j+1)*LogVols(n-j+1))^2
    end
end
f = sum(E)

The problem is that that I just get f=0. The variables declared seem to be fine.
Would greatly appreciate any help.

Thanks!

Subject: Fucntion with loops

From: Bruno Luong

Date: 15 Dec, 2008 06:48:02

Message: 2 of 6

"Benp P" <lightatron@hotmail.com> wrote in message <gi4u26$ith$1@fred.mathworks.com>...
> Hi,,
>
> I am trying to write a function takes a vector a as its input, and returns a scalar obtained by summing a series of products of the elements of x such as
>
> function f = vsquare(x)
> global L;
> global Weights;
> global n;
> global LogVols;
>
> term = 0;
> E = zeros(1,n*(n+1)/2);
> for i = 1:n
> for j = 1:i
> term = term + 1;
> E(term) = (L(n-i+1,n-j+1)*x(n-j+1)*Weights(n-j+1)*LogVols(n-j+1))^2
> end
> end
> f = sum(E)
>
> The problem is that that I just get f=0. The variables declared seem to be fine.
> Would greatly appreciate any help.
>

Use a debugger to set a break point inside the loop (line E(term) ...) then run and inspect what is wrong.

Bruno

Subject: Fucntion with loops

From: Nasser Abbasi

Date: 15 Dec, 2008 07:23:59

Message: 3 of 6


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:gi4uj2$mbh$1@fred.mathworks.com...

>
> Use a debugger to set a break point inside the loop (line E(term) ...)
> then run and inspect what is wrong.
>

It seems to me that some users of Matlab do not even know that there exists
a debugger in Matlab which could be used to answer 99% of the questions
posted :)

--Nasser

Subject: Fucntion with loops

From: Justus Skorps

Date: 15 Dec, 2008 08:06:13

Message: 4 of 6


>
> It seems to me that some users of Matlab do not even know that there exists
> a debugger in Matlab which could be used to answer 99% of the questions
> posted :)
>
> --Nasser

I am sure many know that that but it is much easier to let other do
this work with posting the questions...

Subject: Fucntion with loops

From: Pekka Kumpulainen

Date: 15 Dec, 2008 08:59:02

Message: 5 of 6

Justus Skorps <jussa@gmx.de> wrote in message <44554987-ca21-436f-9694-2cabd44efb91@k24g2000pri.googlegroups.com>...
>
> >
> > It seems to me that some users of Matlab do not even know that there exists
> > a debugger in Matlab which could be used to answer 99% of the questions
> > posted :)
> >
> > --Nasser
>
> I am sure many know that that but it is much easier to let other do
> this work with posting the questions...

In many cases it would be even easier and faster to type half of the subject line of the post in the search box in help. But then you would have to read all those few lines in the documentation...
The net and this group especially are great to have around (I learn new things here everyday) but somehow people seem to shout for help without trying much first.

"Present-day people don't have the courage to get stuck in ordinary everyday tasks" - unprofessional translation from a finnish comic strip
.. and it's only monday...

Subject: Fucntion with loops

From: Adam

Date: 15 Dec, 2008 14:16:01

Message: 6 of 6

"Benp P" <lightatron@hotmail.com> wrote in message <gi4u26$ith$1@fred.mathworks.com>...
> Hi,,
>
> function f = vsquare(x)
> global L;
> global Weights;
> global n;
> global LogVols;

This makes me cringe. Either input all of them

function f = vsquare(x, L, Weights, n, LogVols)

-----

or build a parameter and pass that into the function

stuff.L = L;
...
stuff.LogVols = LogVols;

vsquare(x, stuff)

~Adam

Tags for 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