Its very easy to get the value of π. As π is a floating point number declare a long variable then assign 'pi' to that long variable you will get the value.
Why not try it? Before you do, why not spend some time with the basic tutorials?
Assuming you want to comute an inverse sine function,
help asin
How do you square something? Well, you already know how to do that. And, as far as entering pi into MATLAB, it already has pi as a variable by defaiult, as you should have learned from reading the answers to this question.
If your question is to compute the sin of the inverse of pi/6, again, you can do so directly. Can you compute the inverse of a fraction? Can you compute the sine of that? Can you compute the square of that result?
help sin
So in either case for your ambiguous question, you solve it by writing it in pieces, in steps, one step at a time until you have your result.
Attached is code to compute Ramanujan's formula for pi, voted the ugliest formula of all time.
.
Actually I think it's amazing that something analytical that complicated and with a variety of operations (addition, division, multiplication, factorial, square root, exponentiation, and summation) could create something as "simple" as pi.
Unfortunately it seems to get to within MATLAB's precision after just one iteration - I'd have like to see how it converges as afunction of iteration (summation term). (Hint: help would be appreciated.)
As I recall, these approximations tend to give a roughly fixed number of digits per term. I'll do it using HPF, but syms would also work.
DefaultNumberOfDigits 500
n = 10;
piterms = zeros(n+1,1,'hpf');
f = sqrt(hpf(2))*2/9801*hpf(factorial(0));
piterms(1) = f*1103;
hpf396 = hpf(396)^4;
for k = 1:n
hpfk = hpf(k);
f = f*(4*hpfk-3)*(4*hpfk-2)*(4*hpfk-1)*4/(hpfk^3)/hpf396;
piterms(k+1) = f*(1103 + 26390*hpfk);
end
piapprox = 1./cumsum(piterms);
pierror = double(hpf('pi') - piapprox))
pierror =
-7.6424e-08
-6.3954e-16
-5.6824e-24
-5.2389e-32
-4.9442e-40
-4.741e-48
-4.5989e-56
-4.5e-64
-4.4333e-72
-4.3915e-80
-4.3696e-88
So roughly 8 digits per term in this series. Resetting the default number of digits to used to 1000, then n=125, so a total of 126 terms in the series, we can pretty quickly get a 1000 digit approximation to pi:
pierror = hpf('pi') - piapprox(end + [-3:0])
pierror =
HPF array of size: 41
|1,1| -1.2060069282720814803655e-982
|2,1| -1.25042729756426e-990
|3,1| -1.296534e-998
|4,1| -8.e-1004
So as you see, it generates a very reliable 8 digits per term in the sum.
I also ran it for 100000 digits, so 12500 terms. It took a little more time, but was entirely possible to compute. I don't recall which similar approximation I used some time ago, but I once used it to compute 1 million or so digits of pi in HPF. HPF currently stores a half million digits as I recall.
As far as understanding how to derive that series, I would leave that to Ramanujan, and only hope he is listening on on this.
MATLAB changed the rules in R2020a (I think it was). sym('pi') now creates a symbolic variable named pi that is nothing special. To create the symbolic π now, you should use
sym(pi)
ans =
π
and count on the fact that sym() recognizes values "close" to pi as being π
That's correct. There are four different conversion techniques the sym function uses to determine how to convert a number into a symbolic expression. The default is the 'r' flag which as the documentation states "converts floating-point numbers obtained by evaluating expressions of the form p/q, p*pi/q, sqrt(p), 2^q, and 10^q (for modest sized integers p and q) to the corresponding symbolic form."
The value returned by the pi function is "close enough" to p*pi/q (with p and q both equal to 1) for that conversion technique to recognize it as π. If you wanted the numeric value of the symbolic π to some number of decimal places use vpa.
No se puede completar la acción debido a los cambios realizados en la página. Vuelva a cargar la página para ver el estado actualizado.
Translated by
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.