how do I write 2^-x in MatLab?

답변 (2개)

VBBV
VBBV 2023년 2월 17일
편집: VBBV 2023년 2월 17일

1 개 추천

x=1:10; % if you have x as vector of values
2.^(-x) % then use .^ element wise power
ans = 1×10
0.5000 0.2500 0.1250 0.0625 0.0312 0.0156 0.0078 0.0039 0.0020 0.0010
% 2nd option is straightforward
x = 2
x = 2
2^(-x)
ans = 0.2500
%To insert in a given fnction
x = 1:0.1:10;
func = @(x) exp(x)+2.^(-x)+2*cos(x)
func = function_handle with value:
@(x)exp(x)+2.^(-x)+2*cos(x)
plot(func(x))
Fangjun Jiang
Fangjun Jiang 2023년 2월 17일

0 개 추천

x=1;
2^(-x)
ans = 0.5000

댓글 수: 1

John D'Errico
John D'Errico 2023년 2월 17일
편집: John D'Errico 2023년 2월 17일
The parens are not technically needed there, but they don't hurt anything.
x = 1;
2^-x
ans = 0.5000
And to be honest, I usually put a parens in something like that myself just to make it easier to read. Sometimes spare parens make things more clear, and sometimes they just create a confusing mess, creating a parenthetical balancing act.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2023년 2월 17일

편집:

2023년 2월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by