ans = 
How to input pi
이전 댓글 표시
How can i enter pi into an equation on matlab?
댓글 수: 4
Vignesh Shetty
2020년 4월 6일
Hi Anthony!
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.
Eg:-
format long
p=pi
Walter Roberson
2022년 12월 16일
That is what @Geoff Hayes suggested years before. But it does not enter π into the calculation, only an approximation of π
ARSHIYA
2025년 2월 1일
What is the code for y={sin inv(π/6)}^2
John D'Errico
2025년 2월 1일
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.
So try it!
채택된 답변
추가 답변 (6개)
Essam Aljahmi
2018년 5월 31일
편집: Walter Roberson
2018년 5월 31일
28t2e−0.3466tcos(0.6πt+π3)ua(t).
댓글 수: 5
Walter Roberson
2018년 5월 31일
편집: Walter Roberson
2018년 5월 31일
Guessing a number of times about what that is intended to mean:
28 * t.^2 .* exp(−0.3466 .* t .* cos(0.6 .* pi .* t + pi/3) .* ua(t))
James Tursa
2018년 5월 31일
How is this answer relevant?
Roger Pou
2018년 10월 20일
because it is maybe a joke. maybe its the same value as pi but with a long formula.
Image Analyst
2018년 10월 20일
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.)
John D'Errico
2018년 11월 28일
편집: John D'Errico
2018년 11월 28일
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: 4 1
|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.
piapprox(end)
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199
hpf('pi')
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420199
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.
Walter Roberson
2018년 10월 20일
1 개 추천
If you are constructing an equation using the symbolic toolbox use sym('pi')
댓글 수: 3
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)
and count on the fact that sym() recognizes values "close" to pi as being π
James Emmanuelle Galvan
2021년 10월 22일
sym(pi) prints out "pi".
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.
p = sym(pi)
vpa(p, 30)
Dmitry Volkov
2022년 12월 16일
0 개 추천
Easy way:
format long
p = pi
댓글 수: 1
Walter Roberson
2022년 12월 16일
That is what @Geoff Hayes suggested years before. But it does not enter π into the calculation, only an approximation of π
AKHIL TONY
2023년 8월 1일
0 개 추천
using pi will give an approximate value
댓글 수: 1
Walter Roberson
2023년 8월 1일
Yes, multiple people pointed that out years ago
Meghpara
2024년 7월 27일
0 개 추천
it is easy to ge pi
in p=PI.
댓글 수: 1
p=PI
If you meant
p=pi
then @Vignesh Shetty suggested exactly that https://www.mathworks.com/matlabcentral/answers/303687-how-to-input-pi#comment_822235 several years ago, which in turn is functionally equivalent to what @Geoff Hayes suggested in 2016 https://www.mathworks.com/matlabcentral/answers/303687-how-to-input-pi#answer_235320
카테고리
도움말 센터 및 File Exchange에서 Elementary Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!