Thread Subject: Beginner's loop

Subject: Beginner's loop

From: Matlab Beginner

Date: 25 Oct, 2009 20:48:29

Message: 1 of 3

hello everybody.

I am a beginner in matlab and I encounter some problems when
programming loops. I would like to understand what I do wrong and how
can I make it better. I hope there somebody can help me with this.

the problem is the following:

I need to write a function with branches that I need to use afterward
in computing some optimal values.
1. I wrote first a function file for the branching function:

function [c] = c1(x);

for i = 1:size(x)
    if x(i)>=0.5
        c(i) = (x(i)>=0.5)^2;
    else
        c(i) = x(i)-1;
    end
end

in principle I want it to go through all the elements of a vector that
I will give in later and evaluate it using the resp. expression.

2.Testing the function first: when In the main file I write:

x=linspace(-1,10,100)

c1(x)...

it evaluates only x(1)=-1... what am I doing wrong?

3. What I actually need is to use the function in this function file:
(e is a vector of variables e(1) and e(2))

function [firstordcond1, firstordcond2] = nest1seller(e,el,g,mu,b,Bet)

firstordcond2 = c1(e)-((Bet*b*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)
*b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));
firstordcond1 = c1(2)-((Bet*g*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)
*b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));
end


could anybody be so kind and help me with this thing?

many many thanks
a matlab beginner

Subject: Beginner's loop

From: Phil Goddard

Date: 26 Oct, 2009 01:24:01

Message: 2 of 3

Given your input vector,
>> for i = 1:size(x)
is equivalent to
>> for i = 1:[1 100]
which is equivalent to
>> for i = 1:1

Hence you are only getting one pass through the loop.

Assuming x is always a vector you want
>> for i = 1:length(x)

Phil.

Subject: Beginner's loop

From: NZTideMan

Date: 26 Oct, 2009 03:47:21

Message: 3 of 3

On Oct 26, 9:48 am, Matlab Beginner <matlabegin...@googlemail.com>
wrote:
> hello everybody.
>
> I am a beginner in matlab and I encounter some problems when
> programming loops. I would like to understand what I do wrong and how
> can I make it better. I hope there somebody can help me with this.
>
> the problem is the following:
>
> I need to write a function with branches that I need to use afterward
> in computing some optimal values.
> 1. I wrote first a function file for the branching function:
>
> function [c] = c1(x);
>
> for i = 1:size(x)
>     if x(i)>=0.5
>         c(i) = (x(i)>=0.5)^2;
>     else
>         c(i) = x(i)-1;
>     end
> end
>
> in principle I want it to go through all the elements of a vector that
> I will give in later and evaluate it using the resp. expression.
>
> 2.Testing the function first: when In the main file I write:
>
> x=linspace(-1,10,100)
>
> c1(x)...
>
> it evaluates only x(1)=-1... what am I doing wrong?
>
> 3. What I actually need is to use the function in this function file:
> (e is a vector of variables e(1) and e(2))
>
> function [firstordcond1, firstordcond2] = nest1seller(e,el,g,mu,b,Bet)
>
> firstordcond2 = c1(e)-((Bet*b*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)
> *b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));
> firstordcond1 = c1(2)-((Bet*g*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)
> *b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));
> end
>
> could anybody be so kind and help me with this thing?
>
> many many thanks
> a matlab beginner

The great thing about Matlab is that expressions can be vectorised.
This means that for many cases, you don't need a loop at all.
For your case, you can do it like this:
c=x-1; % this creates a vector c with each element being x-1
c(x>=0.5)=(x>=0.5).^2; % Changes just the elements of c where x
>=0.5
Note: I have written this on the RHS:
(x>=0.5).^2;
because it is equivalent to what you have written, but it is nonsense.
Its value is always unity because x>=0.5 is logical 1 and when you
square it you get 1.
Maybe you meant:
(x-0.5).^2;
but who knows?

I glanced at your question 3, but its impenetrable. You'll have to
simplify it.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com