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:
Looping with a function

Subject: Looping with a function

From: Michael Boyd

Date: 29 Feb, 2012 11:16:13

Message: 1 of 6

Hey guys,

was wondering if anyone could help with this simple issue.

I have a function;

function [IN]=closedcrank(crank)
for phase=(1:30)*pi/180;
a=13.5;
l=48;
IN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5-40;
end
end

I want to find the values of 'crank' where the IN is zero.

so I am using ;

x=fzero(@closedcrank,[pi 2*pi])

However, I want matlab to give me values for 'crank' over the varying values of phase. It only gives one value, not the 30 I want.

Any help is appreciated!

Thanks,

Mike

Subject: Looping with a function

From: Nasser M. Abbasi

Date: 29 Feb, 2012 12:13:41

Message: 2 of 6

On 2/29/2012 5:16 AM, Michael Boyd wrote:
> Hey guys,
>
> was wondering if anyone could help with this simple issue.
>
> I have a function;
>
> function [IN]=closedcrank(crank)
> for phase=(1:30)*pi/180;
> a=13.5;
> l=48;
> IN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5-40;
> end
> end
>

That is not a good way to program a loop.

a loop should only iterate over discrete set of values only.

I know, matlab and other loose dynamic languages makes it
easy to develop bad programming habits.

(and why do you put a ";" after the for=...; )


> I want to find the values of 'crank' where the IN is zero.
>
> so I am using ;
>
> x=fzero(@closedcrank,[pi 2*pi])
>
> However, I want matlab to give me values for 'crank' over the varying values of phase.
>It only gives one value, not the 30 I want.
>
> Any help is appreciated!
>
> Thanks,
>
> Mike

I have no idea what you are doing in your loop in the
above function. You loop but I do not see an accumulation
in the loop.

You doing something like

for i=1:100
     X=i^2
end

So, in the end of the loop, only one value is returned.
Are you sure you want a loop?

--Nasser

Subject: Looping with a function

From: Michael Boyd

Date: 29 Feb, 2012 12:22:10

Message: 3 of 6

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jil4pn$5tm$1@speranza.aioe.org>...
> On 2/29/2012 5:16 AM, Michael Boyd wrote:
> > Hey guys,
> >
> > was wondering if anyone could help with this simple issue.
> >
> > I have a function;
> >
> > function [IN]=closedcrank(crank)
> > for phase=(1:30)*pi/180;
> > a=13.5;
> > l=48;
> > IN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5-40;
> > end
> > end
> >
>
> That is not a good way to program a loop.
>
> a loop should only iterate over discrete set of values only.
>
> I know, matlab and other loose dynamic languages makes it
> easy to develop bad programming habits.
>
> (and why do you put a ";" after the for=...; )
>
>
> > I want to find the values of 'crank' where the IN is zero.
> >
> > so I am using ;
> >
> > x=fzero(@closedcrank,[pi 2*pi])
> >
> > However, I want matlab to give me values for 'crank' over the varying values of phase.
> >It only gives one value, not the 30 I want.
> >
> > Any help is appreciated!
> >
> > Thanks,
> >
> > Mike
>
> I have no idea what you are doing in your loop in the
> above function. You loop but I do not see an accumulation
> in the loop.
>
> You doing something like
>
> for i=1:100
> X=i^2
> end
>
> So, in the end of the loop, only one value is returned.
> Are you sure you want a loop?
>
> --Nasser


Ok,

I want to use this piece of code:

 x=fzero(@closedcrank,[pi 2*pi])

To return 30 different values, one for each of the different values of 'phase' in the function. The values of phase are 1:30.

I have little or no programming skills!

I am unsure how to achieve this.

Mike

Subject: Looping with a function

From: Nasser M. Abbasi

Date: 29 Feb, 2012 12:47:27

Message: 4 of 6

On 2/29/2012 6:22 AM, Michael Boyd wrote:
>
> Ok,
>
> I want to use this piece of code:
>
> x=fzero(@closedcrank,[pi 2*pi])
>
> To return 30 different values, one for each of the different values of 'phase' in the function. The values of phase are 1:30.
>
> I have little or no programming skills!
>
> I am unsure how to achieve this.
>
> Mike

fzeros function has to to return a scalar.
and do not use 'l' as variable name, as it looks like the
digit one '1'. use upper case L is you must use 'l' to
make it easier to read.


If I understand what you want, may this below will do it

----------------------------
a = 13.5;
L = 48;

phase = (1:30)*pi/180;
f = @(crank,phi) a*cos(crank-phi)+(L^2-a^2*sin(crank-phi).^2).^0.5-40;

roots = zeros(length(phase),1);

for i = 1:length(phase)
     phi = phase(i);
     roots(i) = fzero(@(x) f(x,phi),0,[pi 2*pi]);
end
------------------------------

roots =

    -2.0575
    -2.0401
    -2.0226
    -2.0052
    -1.9877
    -1.9703
    -1.9528
    -1.9354
    -1.9179
    -1.9005
    -1.8830
    -1.8656
    -1.8481
    -1.8306
    -1.8132
    -1.7957
    -1.7783
    -1.7608
    -1.7434
    -1.7259
    -1.7085
    -1.6910
    -1.6736
    -1.6561
    -1.6387
    -1.6212
    -1.6038
    -1.5863
    -1.5688
    -1.5514

EDU>>

--Nasser

Subject: Looping with a function

From: Michael Boyd

Date: 29 Feb, 2012 13:55:12

Message: 5 of 6

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jil6p1$auo$1@speranza.aioe.org>...
> On 2/29/2012 6:22 AM, Michael Boyd wrote:
> >
> > Ok,
> >
> > I want to use this piece of code:
> >
> > x=fzero(@closedcrank,[pi 2*pi])
> >
> > To return 30 different values, one for each of the different values of 'phase' in the function. The values of phase are 1:30.
> >
> > I have little or no programming skills!
> >
> > I am unsure how to achieve this.
> >
> > Mike
>
> fzeros function has to to return a scalar.
> and do not use 'l' as variable name, as it looks like the
> digit one '1'. use upper case L is you must use 'l' to
> make it easier to read.
>
>
> If I understand what you want, may this below will do it
>
> ----------------------------
> a = 13.5;
> L = 48;
>
> phase = (1:30)*pi/180;
> f = @(crank,phi) a*cos(crank-phi)+(L^2-a^2*sin(crank-phi).^2).^0.5-40;
>
> roots = zeros(length(phase),1);
>
> for i = 1:length(phase)
> phi = phase(i);
> roots(i) = fzero(@(x) f(x,phi),0,[pi 2*pi]);
> end
> ------------------------------
>
> roots =
>
> -2.0575
> -2.0401
> -2.0226
> -2.0052
> -1.9877
> -1.9703
> -1.9528
> -1.9354
> -1.9179
> -1.9005
> -1.8830
> -1.8656
> -1.8481
> -1.8306
> -1.8132
> -1.7957
> -1.7783
> -1.7608
> -1.7434
> -1.7259
> -1.7085
> -1.6910
> -1.6736
> -1.6561
> -1.6387
> -1.6212
> -1.6038
> -1.5863
> -1.5688
> -1.5514
>
> EDU>>
>
> --Nasser

Thank you very much!

Subject: Looping with a function

From: Roger Stafford

Date: 29 Feb, 2012 20:19:16

Message: 6 of 6

"Michael Boyd" wrote in message <jil1dt$b4p$1@newscl01ah.mathworks.com>...
> IN=a*cos(crank-phase)+(l^2-a^2*sin(crank-phase).^2).^0.5-40;
> .......
> x=fzero(@closedcrank,[pi 2*pi])
- - - - - - - -
  Why use 'fzero' when you can use 'acos'? Write your equation this way:

 a*cos(x)+sqrt(b^2-a^2*sin(x)^2) = c

with b = l (I hate lowercase 'l'), x = crank-phase, and c = 40. Then

 sqrt(b^2-a^2*sin(x)^2) = c-a*cos(x)
 b^2-a^2*sin(x)^2 = c^2-2*a*c*cos(x)+a^2*cos(x)^2
 2*a*c*cos(x) = c^2+a^2-b^2
 cos(x) = (c^2+a^2-b^2)/(2*a*c)
 x = acos((c^2+a^2-b^2)/(2*a*c)) or x = -acos((c^2+a^2-b^2)/(2*a*c))

These are the two roots lying between -pi and +pi. All other roots differ from these two by multiples of 2*pi. From these and 'phase' you can derive possible values of 'crank'. You can also use 'acos' in a vectorized version of all this, whereas 'fzero' is incapable of this.

Roger Stafford

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