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:
vpa help with matrices

Subject: vpa help with matrices

From: ben payne

Date: 18 Apr, 2010 16:06:05

Message: 1 of 7

I have a Fortran 90 program in which I perform many matrix multiplications. I would like to find a product with a user-defined precision. To do this, I am using vpa(). I cannot use sym() due to memory constraints of the program. The problem I have is in how to use vpa() correctly. For example, suppose I want to perform the following multiplication, accurate to 100 decimal places:

x=[sqrt(2) sqrt(3); sqrt(5) sqrt(6)]
y=[sqrt(7); sqrt(8)]
z=[sqrt(14)+sqrt(24); sqrt(35)+sqrt(48)]
a=x*y-z

Without vpa() used in determining x,y,z, then I get

>> vpa(a,100)
ans =
 0.0000000000000017763568394002504646778106689453125
 0

I want to be able to specify the precision of the answer for a, but the following does not do what I want:

x=vpa([sqrt(2) sqrt(3); sqrt(5) sqrt(6)],100)
y=vpa([sqrt(7); sqrt(8)],100)
z=vpa([sqrt(14)+sqrt(24); sqrt(35)+sqrt(48)],100)
a=vpa(x*y-z,100)

Nor does this work:

a=vpa(vpa(x,100)*vpa(y,100)-vpa(z,100),100)

Please let me know if further clarification is needed.

Thanks in advance.

Subject: vpa help with matrices

From: Roger Stafford

Date: 18 Apr, 2010 17:13:05

Message: 2 of 7

"ben payne" <ben.is.located@gmail.com> wrote in message <hqfald$g20$1@fred.mathworks.com>...
> I have a Fortran 90 program in which I perform many matrix multiplications. I would like to find a product with a user-defined precision. To do this, I am using vpa(). I cannot use sym() due to memory constraints of the program. The problem I have is in how to use vpa() correctly. For example, suppose I want to perform the following multiplication, accurate to 100 decimal places:
> .......

  The 'vpa' function merely converts a symbolic expression into a decimal string with the accuracy of the 'digits' or 'vpa' setting. When you write

 vpa(sqrt(2),100) ,

unless '2' is of 'sym' form, all you get is the sixteen-place accuracy of double precision floating point for the square root but then converted to the 100 decimals in the vpa setting. That does you no good. You cannot avoid the use of 'sym' if you want more accuracy than that of the ordinary double precision floating point.

Roger Stafford

Subject: vpa help with matrices

From: ben payne

Date: 18 Apr, 2010 17:42:04

Message: 3 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hqfej0$fj4$1@fred.mathworks.com>...
> "ben payne" <ben.is.located@gmail.com> wrote in message <hqfald$g20$1@fred.mathworks.com>...
> > I have a Fortran 90 program in which I perform many matrix multiplications. I would like to find a product with a user-defined precision. To do this, I am using vpa(). I cannot use sym() due to memory constraints of the program. The problem I have is in how to use vpa() correctly. For example, suppose I want to perform the following multiplication, accurate to 100 decimal places:
> > .......
>
> The 'vpa' function merely converts a symbolic expression into a decimal string with the accuracy of the 'digits' or 'vpa' setting. When you write
>
> vpa(sqrt(2),100) ,
>
> unless '2' is of 'sym' form, all you get is the sixteen-place accuracy of double precision floating point for the square root but then converted to the 100 decimals in the vpa setting. That does you no good. You cannot avoid the use of 'sym' if you want more accuracy than that of the ordinary double precision floating point.
>
> Roger Stafford

Hello. I am somewhat confused as to how useful vpa is if it only displays output from symbolic computation. For example, if I want to perform a numeric calculation to better than double or quad precision, it sounds like matlab is unable to do that?

I am asking basically the same question as on
http://www.mathworks.com/support/solutions/en/data/1-1AGHW/index.html?product=SM&solution=1-1AGHW
but they don't answer my question there. Namely, if I have two variables known to 100 decimal places, can I perform operations with those variables and retain precision?

Unlike thread 151496, I don't need the fastest library. The original program is written in Fortran 90, and I have used quadruple precision for that. However, quadruple precision is insufficient.

Do I need to be using something like the following?
http://www.mathworks.com/matlabcentral/fileexchange/6446
I assumed matlab would be able to perform vpa arithmetic.

Subject: vpa help with matrices

From: Roger Stafford

Date: 18 Apr, 2010 18:32:07

Message: 4 of 7

"ben payne" <ben.is.located@gmail.com> wrote in message <hqfg9c$bii$1@fred.mathworks.com>...
> Hello. I am somewhat confused as to how useful vpa is if it only displays output from symbolic computation. For example, if I want to perform a numeric calculation to better than double or quad precision, it sounds like matlab is unable to do that?
>
> I am asking basically the same question as on
> http://www.mathworks.com/support/solutions/en/data/1-1AGHW/index.html?product=SM&solution=1-1AGHW
> but they don't answer my question there. Namely, if I have two variables known to 100 decimal places, can I perform operations with those variables and retain precision?
>
> Unlike thread 151496, I don't need the fastest library. The original program is written in Fortran 90, and I have used quadruple precision for that. However, quadruple precision is insufficient.
>
> Do I need to be using something like the following?
> http://www.mathworks.com/matlabcentral/fileexchange/6446
> I assumed matlab would be able to perform vpa arithmetic.

  On my system if I type

 x = vpa('sqrt(2)',100)

I get the square root of two out to a hundred decimal places of accuracy, but if I merely type

 y = vpa(sqrt(2),100)

I get a hundred decimal places but it is accurate only out to the sixteenth place.

  The difference is that I have convinced 'vpa' to regard the first expression as being of type 'sym', but the second is only regarded as of type 'double'. There is a world of difference between the two entities.

  To obtain 100-place accuracy in general computations you will need a system that is capable of handling type 'sym'. When you say, "I have two variables known to 100 decimal places" you cannot be talking about a variable represented as a 64-bit double precision floating point number. It can't be done with such a limited object. It has to be in another form with far greater capacity, a form which the symbolic toolbox can use, which is to say a type 'sym'. As you are probably aware, matlab's symbolic toolbox accomplishes this with strings of characters or strings of decimal digits of variable lengths depending on the required accuracy.

Roger Stafford

Subject: vpa help with matrices

From: ben payne

Date: 18 Apr, 2010 19:05:10

Message: 5 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hqfj77$p87$1@fred.mathworks.com>...
> "ben payne" <ben.is.located@gmail.com> wrote in message <hqfg9c$bii$1@fred.mathworks.com>...
> > Hello. I am somewhat confused as to how useful vpa is if it only displays output from symbolic computation. For example, if I want to perform a numeric calculation to better than double or quad precision, it sounds like matlab is unable to do that?
> >
> > I am asking basically the same question as on
> > http://www.mathworks.com/support/solutions/en/data/1-1AGHW/index.html?product=SM&solution=1-1AGHW
> > but they don't answer my question there. Namely, if I have two variables known to 100 decimal places, can I perform operations with those variables and retain precision?
> >
> > Unlike thread 151496, I don't need the fastest library. The original program is written in Fortran 90, and I have used quadruple precision for that. However, quadruple precision is insufficient.
> >
> > Do I need to be using something like the following?
> > http://www.mathworks.com/matlabcentral/fileexchange/6446
> > I assumed matlab would be able to perform vpa arithmetic.
>
> On my system if I type
>
> x = vpa('sqrt(2)',100)
>
> I get the square root of two out to a hundred decimal places of accuracy, but if I merely type
>
> y = vpa(sqrt(2),100)
>
> I get a hundred decimal places but it is accurate only out to the sixteenth place.
>
> The difference is that I have convinced 'vpa' to regard the first expression as being of type 'sym', but the second is only regarded as of type 'double'. There is a world of difference between the two entities.
>
> To obtain 100-place accuracy in general computations you will need a system that is capable of handling type 'sym'. When you say, "I have two variables known to 100 decimal places" you cannot be talking about a variable represented as a 64-bit double precision floating point number. It can't be done with such a limited object. It has to be in another form with far greater capacity, a form which the symbolic toolbox can use, which is to say a type 'sym'. As you are probably aware, matlab's symbolic toolbox accomplishes this with strings of characters or strings of decimal digits of variable lengths depending on the required accuracy.
>
> Roger Stafford

Thanks for your help Roger. I will implement the program in Mathematica
http://reference.wolfram.com/mathematica/tutorial/ArbitraryPrecisionNumbers.html

Subject: vpa help with matrices

From: Steven Lord

Date: 19 Apr, 2010 15:50:23

Message: 6 of 7


"ben payne" <ben.is.located@gmail.com> wrote in message
news:hqfg9c$bii$1@fred.mathworks.com...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
> message <hqfej0$fj4$1@fred.mathworks.com>...
>> "ben payne" <ben.is.located@gmail.com> wrote in message
>> <hqfald$g20$1@fred.mathworks.com>...
>> > I have a Fortran 90 program in which I perform many matrix
>> > multiplications. I would like to find a product with a user-defined
>> > precision. To do this, I am using vpa(). I cannot use sym() due to
>> > memory constraints of the program. The problem I have is in how to use
>> > vpa() correctly. For example, suppose I want to perform the following
>> > multiplication, accurate to 100 decimal places:
>> > .......
>>
>> The 'vpa' function merely converts a symbolic expression into a decimal
>> string with the accuracy of the 'digits' or 'vpa' setting. When you
>> write
>>
>> vpa(sqrt(2),100) ,
>>
>> unless '2' is of 'sym' form, all you get is the sixteen-place accuracy of
>> double precision floating point for the square root but then converted to
>> the 100 decimals in the vpa setting. That does you no good. You cannot
>> avoid the use of 'sym' if you want more accuracy than that of the
>> ordinary double precision floating point.
>>
>> Roger Stafford
>
> Hello. I am somewhat confused as to how useful vpa is if it only displays
> output from symbolic computation. For example, if I want to perform a
> numeric calculation to better than double or quad precision, it sounds
> like matlab is unable to do that?

To perform a calculation to greater than double precision, use the
arbitrary-precision arithmetic in Symbolic Math Toolbox.

two = sym(2);
squareRootOfTwo = sqrt(two)
vpa(squareRootOfTwo, 100) % Display it to 100 decimal places.

> I am asking basically the same question as on
> http://www.mathworks.com/support/solutions/en/data/1-1AGHW/index.html?product=SM&solution=1-1AGHW
> but they don't answer my question there. Namely, if I have two variables
> known to 100 decimal places, can I perform operations with those variables
> and retain precision?

So you type the following into MATLAB:

x = 1.234567890123456789012345678901234567890123456789012345678901234567890

If you do that, MATLAB will evaluate that expression _in double precision_
and store the result in the _double precision_ variable x.

> Unlike thread 151496, I don't need the fastest library. The original
> program is written in Fortran 90, and I have used quadruple precision for
> that. However, quadruple precision is insufficient.
>
> Do I need to be using something like the following?
> http://www.mathworks.com/matlabcentral/fileexchange/6446
> I assumed matlab would be able to perform vpa arithmetic.

See above.

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

Subject: vpa help with matrices

From: benbarrowes

Date: 20 Apr, 2010 02:10:44

Message: 7 of 7

On Apr 18, 3:05 pm, "ben payne" <ben.is.loca...@gmail.com> wrote:
> "Roger Stafford" <ellieandrogerxy...@mindspring.com.invalid> wrote in message <hqfj77$p8...@fred.mathworks.com>...
> > "ben payne" <ben.is.loca...@gmail.com> wrote in message <hqfg9c$bi...@fred.mathworks.com>...
> > > Hello. I am somewhat confused as to how useful vpa is if it only displays output from symbolic computation. For example, if I want to perform a numeric calculation to better than double or quad precision, it sounds like matlab is unable to do that?
>
> > > I am asking basically the same question as on
> > >http://www.mathworks.com/support/solutions/en/data/1-1AGHW/index.html...
> > > but they don't answer my question there. Namely, if I have two variables known to 100 decimal places, can I perform operations with those variables and retain precision?
>
> > > Unlike thread 151496, I don't need the fastest library. The original program is written in Fortran 90, and I have used quadruple precision for that. However, quadruple precision is insufficient.
>
> > > Do I need to be using something like the following?
> > >http://www.mathworks.com/matlabcentral/fileexchange/6446
> > > I assumed matlab would be able to perform vpa arithmetic.
>
> >   On my system if I type
>
> >  x = vpa('sqrt(2)',100)
>
> > I get the square root of two out to a hundred decimal places of accuracy, but if I merely type
>
> >  y = vpa(sqrt(2),100)
>
> > I get a hundred decimal places but it is accurate only out to the sixteenth place.
>
> >   The difference is that I have convinced 'vpa' to regard the first expression as being of type 'sym', but the second is only regarded as of type 'double'.  There is a world of difference between the two entities.
>
> >   To obtain 100-place accuracy in general computations you will need a system that is capable of handling type 'sym'.  When you say, "I have two variables known to 100 decimal places" you cannot be talking about a variable represented as a 64-bit double precision floating point number.  It can't be done with such a limited object.  It has to be in another form with far greater capacity, a form which the symbolic toolbox can use, which is to say a type 'sym'.  As you are probably aware, matlab's symbolic toolbox accomplishes this with strings of characters or strings of decimal digits of variable lengths depending on the required accuracy.
>
> > Roger Stafford
>
> Thanks for your help Roger. I will implement the program in Mathematicahttp://reference.wolfram.com/mathematica/tutorial/ArbitraryPrecisionN...

You can also use the free multiple precision toolbox in matlab:
http://www.mathworks.com/matlabcentral/fileexchange/6446-multiple-precision-toolbox-for-matlab

Ben Barrowes

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