Ok so i have a function to write and it is supposed to calculate and return the product of 1 to n in a step size of 2.
for n = 1:2
inv(1*3*5*7);
end
How do i make this a step size of two? Do i do for n = 1:n:2?

댓글 수: 3

Star Strider
Star Strider 2014년 4월 27일
The documentation on the colon(:) operator will get you started.
Tony Hill
Tony Hill 2014년 4월 27일
ok i changed it to
For n = 1:n:2 and i keep getting a syntax error about variable n being used before it was assigned
You’re getting there.
From the documentation:
Using two colons to create a vector with arbitrary real increments between the elements,
E = 0:.1:.5
results in
E =
0 0.1000 0.2000 0.3000 0.4000 0.5000

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

답변 (2개)

Geoff Hayes
Geoff Hayes 2014년 4월 27일

0 개 추천

Hi Tony,
Either type help for in the MATLAB command window to get information on for loop syntax, or visit the following link: for loop.
Geoff
Image Analyst
Image Analyst 2014년 4월 28일

0 개 추천

No for loop is needed at all. See the prod() function.
And you got the colon stuff in the wrong order. The correct order is startingValue : stepAmount : endingValue. You put the step at the end instead of the middle.

댓글 수: 1

Anyway, that problem is trivial, toddler-level stuff. Here's a far more interesting question for you:
Investigate these two different equations with different values of n:
sum1 = 1^n + 19^n + 20^n + 51^n + 57^n + 80^n + 82^n
sum2 = 2^n + 12^n + 31^n + 40^n + 69^n + 71^n + 85^n
Explain what you see and why you see it.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2014년 4월 27일

댓글:

2014년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by