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:
How do is the calculation done? MatLab gives two different values HIGH IMPORTANCE

Subject: How do is the calculation done? MatLab gives two different values HIGH IMPORTANCE

From: Saga

Date: 21 Feb, 2010 14:00:22

Message: 1 of 7

Hi,

I've got matlab to generate a equation of polynomial.

a = 0.0015 -0.1084 3.8509 -35.1232

which is of order 3 i.e.

0.0015*x^3 - 0.1084*x^2 + 3.8509*x - 35.1232

when I type on matlab: polyval (a,19.13)

the answer is: 9.4393

x being the value of 19.13,

But when I compute the value on matlab manually:

0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232

The value it returns is 9.3759

How is this possible when, is there a mistake I'm making while calculating the formula manually?

Please advice.

Thanks

Subject: How do is the calculation done? MatLab gives two different values

From: dpb

Date: 21 Feb, 2010 14:21:33

Message: 2 of 7

Saga wrote:
> Hi,
>
> I've got matlab to generate a equation of polynomial.
>
> a = 0.0015 -0.1084 3.8509 -35.1232
>
> which is of order 3 i.e.
>
> 0.0015*x^3 - 0.1084*x^2 + 3.8509*x - 35.1232
>
> when I type on matlab: polyval (a,19.13)
>
> the answer is: 9.4393
> x being the value of 19.13,
> But when I compute the value on matlab manually:
>
> 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
>
> The value it returns is 9.3759
>
> How is this possible when, is there a mistake I'm making while
> calculating the formula manually?
>
> Please advice.

 >> a =[ 0.0015 -0.1084 3.8509 -35.1232]
a =
     0.0015 -0.1084 3.8509 -35.1232
 >> polyval (a,19.13)
ans =
     9.3759
 >>

Now what do you think is the problem/answer/cause...?

--

Subject: How do is the calculation done? MatLab gives two different values

From: Saga

Date: 21 Feb, 2010 14:41:05

Message: 3 of 7

dpb <none@non.net> wrote in message <hlrfjp$mj1$1@news.eternal-september.org>...
> Saga wrote:
> > Hi,
> >
> > I've got matlab to generate a equation of polynomial.
> >
> > a = 0.0015 -0.1084 3.8509 -35.1232
> >
> > which is of order 3 i.e.
> >
> > 0.0015*x^3 - 0.1084*x^2 + 3.8509*x - 35.1232
> >
> > when I type on matlab: polyval (a,19.13)
> >
> > the answer is: 9.4393
> > x being the value of 19.13,
> > But when I compute the value on matlab manually:
> >
> > 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
> >
> > The value it returns is 9.3759
> >
> > How is this possible when, is there a mistake I'm making while
> > calculating the formula manually?
> >
> > Please advice.
>
> >> a =[ 0.0015 -0.1084 3.8509 -35.1232]
> a =
> 0.0015 -0.1084 3.8509 -35.1232
> >> polyval (a,19.13)
> ans =
> 9.3759
> >>
>
> Now what do you think is the problem/answer/cause...?
>
> --

When I do:

>> a = polyfit(x,y,3)

a =

    0.0015 -0.1084 3.8509 -35.1232

>> polyval(a,19.13)

ans =

    9.4393

It is a different value when you assign values of "a" manually compared to using the polyfit.
So then I try to work out how the polyval method is calculating the equation of a

i.e.
>> 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232

ans =

    9.3759

Why are there different values?
Is the formula which I typed manually incorrect?

Thanks

Subject: How do is the calculation done? MatLab gives two different values

From: Sadik

Date: 21 Feb, 2010 15:04:05

Message: 4 of 7

Saga,

Please type

format long

and then, redo your

a = polyfit

stuff. You will see that your coefficients are not as sharp as 0.0015. It could for example be 0.0015234223 or something like that. This is why you are getting a different answer.

By default, matlab rounds the numbers to display them with 4 decimal places.

Best.




"Saga " <a5stargamescompany@googlemail.com> wrote in message <hlrgm1$qrm$1@fred.mathworks.com>...
> dpb <none@non.net> wrote in message <hlrfjp$mj1$1@news.eternal-september.org>...
> > Saga wrote:
> > > Hi,
> > >
> > > I've got matlab to generate a equation of polynomial.
> > >
> > > a = 0.0015 -0.1084 3.8509 -35.1232
> > >
> > > which is of order 3 i.e.
> > >
> > > 0.0015*x^3 - 0.1084*x^2 + 3.8509*x - 35.1232
> > >
> > > when I type on matlab: polyval (a,19.13)
> > >
> > > the answer is: 9.4393
> > > x being the value of 19.13,
> > > But when I compute the value on matlab manually:
> > >
> > > 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
> > >
> > > The value it returns is 9.3759
> > >
> > > How is this possible when, is there a mistake I'm making while
> > > calculating the formula manually?
> > >
> > > Please advice.
> >
> > >> a =[ 0.0015 -0.1084 3.8509 -35.1232]
> > a =
> > 0.0015 -0.1084 3.8509 -35.1232
> > >> polyval (a,19.13)
> > ans =
> > 9.3759
> > >>
> >
> > Now what do you think is the problem/answer/cause...?
> >
> > --
>
> When I do:
>
> >> a = polyfit(x,y,3)
>
> a =
>
> 0.0015 -0.1084 3.8509 -35.1232
>
> >> polyval(a,19.13)
>
> ans =
>
> 9.4393
>
> It is a different value when you assign values of "a" manually compared to using the polyfit.
> So then I try to work out how the polyval method is calculating the equation of a
>
> i.e.
> >> 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
>
> ans =
>
> 9.3759
>
> Why are there different values?
> Is the formula which I typed manually incorrect?
>
> Thanks

Subject: How do is the calculation done? MatLab gives two different values

From: dpb

Date: 21 Feb, 2010 15:19:03

Message: 5 of 7

Saga wrote:
...

>> > > when I type on matlab: polyval (a,19.13)
>> > > the answer is: 9.4393
>> > x being the value of 19.13,
>> > But when I compute the value on matlab manually:
>> > > 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
>> > > The value it returns is 9.3759
>> > > How is this possible when, is there a mistake I'm making while >
>> calculating the formula manually?
>> > > Please advice.
>>
>> >> a =[ 0.0015 -0.1084 3.8509 -35.1232]
>> a =
>> 0.0015 -0.1084 3.8509 -35.1232
>> >> polyval (a,19.13)
>> ans =
>> 9.3759
>> >>
>>
>> Now what do you think is the problem/answer/cause...?
>>
>> --
>
> When I do:
>
>>> a = polyfit(x,y,3)
>
> a =
>
> 0.0015 -0.1084 3.8509 -35.1232
>
>>> polyval(a,19.13)
>
> ans =
>
> 9.4393
>

You said that already...I asked you to really think about why that might
be...

> It is a different value when you assign values of "a" manually compared
> to using the polyfit.

Yes, but are those the _SAME_ values of a to full double precision???

> So then I try to work out how the polyval method is calculating the
> equation of a
>
> i.e.
>>> 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
>
> ans =
>
> 9.3759
>
> Why are there different values?
> Is the formula which I typed manually incorrect?

Consider the numerics more closely

hint

try

polyfit(x,y,3) - a

where 'a' is the manually entered coefficient vector above.

--

Subject: How do is the calculation done? MatLab gives two different values

From: Saga

Date: 21 Feb, 2010 17:34:04

Message: 6 of 7

"Sadik " <sadik.hava@gmail.com> wrote in message <hlri15$flt$1@fred.mathworks.com>...
> Saga,
>
> Please type
>
> format long
>
> and then, redo your
>
> a = polyfit
>
> stuff. You will see that your coefficients are not as sharp as 0.0015. It could for example be 0.0015234223 or something like that. This is why you are getting a different answer.
>
> By default, matlab rounds the numbers to display them with 4 decimal places.
>
> Best.
>
>
>
>
> "Saga " <a5stargamescompany@googlemail.com> wrote in message <hlrgm1$qrm$1@fred.mathworks.com>...
> > dpb <none@non.net> wrote in message <hlrfjp$mj1$1@news.eternal-september.org>...
> > > Saga wrote:
> > > > Hi,
> > > >
> > > > I've got matlab to generate a equation of polynomial.
> > > >
> > > > a = 0.0015 -0.1084 3.8509 -35.1232
> > > >
> > > > which is of order 3 i.e.
> > > >
> > > > 0.0015*x^3 - 0.1084*x^2 + 3.8509*x - 35.1232
> > > >
> > > > when I type on matlab: polyval (a,19.13)
> > > >
> > > > the answer is: 9.4393
> > > > x being the value of 19.13,
> > > > But when I compute the value on matlab manually:
> > > >
> > > > 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
> > > >
> > > > The value it returns is 9.3759
> > > >
> > > > How is this possible when, is there a mistake I'm making while
> > > > calculating the formula manually?
> > > >
> > > > Please advice.
> > >
> > > >> a =[ 0.0015 -0.1084 3.8509 -35.1232]
> > > a =
> > > 0.0015 -0.1084 3.8509 -35.1232
> > > >> polyval (a,19.13)
> > > ans =
> > > 9.3759
> > > >>
> > >
> > > Now what do you think is the problem/answer/cause...?
> > >
> > > --
> >
> > When I do:
> >
> > >> a = polyfit(x,y,3)
> >
> > a =
> >
> > 0.0015 -0.1084 3.8509 -35.1232
> >
> > >> polyval(a,19.13)
> >
> > ans =
> >
> > 9.4393
> >
> > It is a different value when you assign values of "a" manually compared to using the polyfit.
> > So then I try to work out how the polyval method is calculating the equation of a
> >
> > i.e.
> > >> 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
> >
> > ans =
> >
> > 9.3759
> >
> > Why are there different values?
> > Is the formula which I typed manually incorrect?
> >
> > Thanks

Thank You very much...

It works.

Subject: How do is the calculation done? MatLab gives two different values HIGH IMPORTANCE

From: Steven Lord

Date: 22 Feb, 2010 04:05:13

Message: 7 of 7


"Saga " <a5stargamescompany@googlemail.com> wrote in message
news:hlre9m$7eh$1@fred.mathworks.com...
> Hi,
>
> I've got matlab to generate a equation of polynomial.
>
> a = 0.0015 -0.1084 3.8509 -35.1232
>
> which is of order 3 i.e.
>
> 0.0015*x^3 - 0.1084*x^2 + 3.8509*x - 35.1232
>
> when I type on matlab: polyval (a,19.13)
>
> the answer is: 9.4393
> x being the value of 19.13,
> But when I compute the value on matlab manually:
>
> 0.0015*19.13^3 - 0.1084*19.13^2 + 3.8509*19.13 - 35.1232
>
> The value it returns is 9.3759

That's correct. The value of the highest-order coefficient is not exactly
0.0015; it's just being displayed that way, so when you use the truncated
value to evaluate the expression manually, you receive a (slightly)
different result. After all, even though the difference in the coefficient
is small, you're multiplying the truncated coefficient by 19^3 so the small
difference is magnified.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

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