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:
code problem

Subject: code problem

From: Jonathan

Date: 19 Jun, 2012 23:22:08

Message: 1 of 3

Hi,
I'm trying to solve a simple function iteratively. My code is as follows...
d2=3;
z=0.0192;
newvar=zeros(1,1552);
for n = 1:.1:156
    newvar(n) = (d2./n).*tanh((2*pi*d2)./n);
end

The first issue is that Matlab objects to this and gives me the message "Attempted to access newvar(1.1); index must be a positive integer or logical."

Ultimately, what I'm trying to find is the n value that equals the solution to the equation, which is indicated by z. I was planning to do this by using find, once I have run the loop.

What am I doing wrong?

Thanks
Jon

Subject: code problem

From: TideMan

Date: 19 Jun, 2012 23:48:34

Message: 2 of 3

On Wednesday, June 20, 2012 11:22:08 AM UTC+12, Jonathan wrote:
> Hi,
> I'm trying to solve a simple function iteratively. My code is as follows...
> d2=3;
> z=0.0192;
> newvar=zeros(1,1552);
> for n = 1:.1:156
> newvar(n) = (d2./n).*tanh((2*pi*d2)./n);
> end
>
> The first issue is that Matlab objects to this and gives me the message "Attempted to access newvar(1.1); index must be a positive integer or logical."
>
> Ultimately, what I'm trying to find is the n value that equals the solution to the equation, which is indicated by z. I was planning to do this by using find, once I have run the loop.
>
> What am I doing wrong?
>
> Thanks
> Jon

First of all, you cannot proclaim that you are not solving it iteratively.
You are solving it by preparing a look-up table.
Secondly, as the error message has told you, you are using a floating point number as an index - you can't do that.
Here's one way to do it without a loop:
L=[1:0.1:156]; % I'm guessing L is wave length
newvar = (d2./L).*tanh((2*pi*d2)./L);

Now, you cannot use find, but you can use min:
[dum,imn]=min(abs(newvar-z));
and your solution is L(imn)

Subject: code problem

From: Jonathan

Date: 20 Jun, 2012 00:11:07

Message: 3 of 3

TideMan <mulgor@gmail.com> wrote in message <00ccb493-6074-456d-9037-3cd77de9ad5e@googlegroups.com>...
> On Wednesday, June 20, 2012 11:22:08 AM UTC+12, Jonathan wrote:
> > Hi,
> > I'm trying to solve a simple function iteratively. My code is as follows...
> > d2=3;
> > z=0.0192;
> > newvar=zeros(1,1552);
> > for n = 1:.1:156
> > newvar(n) = (d2./n).*tanh((2*pi*d2)./n);
> > end
> >
> > The first issue is that Matlab objects to this and gives me the message "Attempted to access newvar(1.1); index must be a positive integer or logical."
> >
> > Ultimately, what I'm trying to find is the n value that equals the solution to the equation, which is indicated by z. I was planning to do this by using find, once I have run the loop.
> >
> > What am I doing wrong?
> >
> > Thanks
> > Jon
>
> First of all, you cannot proclaim that you are not solving it iteratively.
> You are solving it by preparing a look-up table.
> Secondly, as the error message has told you, you are using a floating point number as an index - you can't do that.
> Here's one way to do it without a loop:
> L=[1:0.1:156]; % I'm guessing L is wave length
> newvar = (d2./L).*tanh((2*pi*d2)./L);
>
> Now, you cannot use find, but you can use min:
> [dum,imn]=min(abs(newvar-z));
> and your solution is L(imn)

Thanks Tideman. Very simple! Appreciate the help.

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