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:
Solve an equation of variable length

Subject: Solve an equation of variable length

From: Hermano Cappa

Date: 4 Feb, 2010 17:35:20

Message: 1 of 4

Hello,

I have no idea how I can write on a compact manner, a program that solves an equation of variable length.

My problem:

from i=1 till n

sum(r_i + x) =0 --> solve to x =?

Example:
n=5;
r=[1;2;3;4;5;6;7];

i= 1 till n

(1 + x) + (2 + x) + (3 + x) + (4 + x) + (5 + x) = 0

---> x = 3

How can I write such a program that gives me the result of x dependent on the value of n?

Subject: Solve an equation of variable length

From: Walter Roberson

Date: 4 Feb, 2010 18:53:09

Message: 2 of 4

Hermano Cappa wrote:

> I have no idea how I can write on a compact manner, a program that
> solves an equation of variable length.
> My problem:

> from i=1 till n
> sum(r_i + x) =0 --> solve to x =?

> Example:
> n=5;
> r=[1;2;3;4;5;6;7];
>
> i= 1 till n
>
> (1 + x) + (2 + x) + (3 + x) + (4 + x) + (5 + x) = 0
> ---> x = 3

Wrong solution: x should be -3, not 3.


Your equation is the same as
solve(sum(r(1:n) + n * x = 0)
which in turn is going to be
n * x = -sum(r(1:n))
which is going to be
x = -1/n * sum(r(1:n))
which is trivial enough not to require solve() at all. Just watch out for
n <= 0.

Subject: Solve an equation of variable length

From: Hermano Cappa

Date: 5 Feb, 2010 09:27:05

Message: 3 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <hkf5hh$k6q$1@canopus.cc.umanitoba.ca>...
> Hermano Cappa wrote:
>
> > I have no idea how I can write on a compact manner, a program that
> > solves an equation of variable length.
> > My problem:
>
> > from i=1 till n
> > sum(r_i + x) =0 --> solve to x =?
>
> > Example:
> > n=5;
> > r=[1;2;3;4;5;6;7];
> >
> > i= 1 till n
> >
> > (1 + x) + (2 + x) + (3 + x) + (4 + x) + (5 + x) = 0
> > ---> x = 3
>
> Wrong solution: x should be -3, not 3.
>
>
> Your equation is the same as
> solve(sum(r(1:n) + n * x = 0)
> which in turn is going to be
> n * x = -sum(r(1:n))
> which is going to be
> x = -1/n * sum(r(1:n))
> which is trivial enough not to require solve() at all. Just watch out for
> n <= 0.

Thank you very much!

Subject: Solve an equation of variable length

From: Hermano Cappa

Date: 5 Feb, 2010 13:50:07

Message: 4 of 4

Walter Roberson <roberson@hushmail.com> wrote in message <hkf5hh$k6q$1@canopus.cc.umanitoba.ca>...
> Hermano Cappa wrote:
>
> > I have no idea how I can write on a compact manner, a program that
> > solves an equation of variable length.
> > My problem:
>
> > from i=1 till n
> > sum(r_i + x) =0 --> solve to x =?
>
> > Example:
> > n=5;
> > r=[1;2;3;4;5;6;7];
> >
> > i= 1 till n
> >
> > (1 + x) + (2 + x) + (3 + x) + (4 + x) + (5 + x) = 0
> > ---> x = 3
>
> Wrong solution: x should be -3, not 3.
>
>
> Your equation is the same as
> solve(sum(r(1:n) + n * x = 0)
> which in turn is going to be
> n * x = -sum(r(1:n))
> which is going to be
> x = -1/n * sum(r(1:n))
> which is trivial enough not to require solve() at all. Just watch out for
> n <= 0.

Thank you very much!

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