Given: Write a function prodby2_username that will recieve a value of a positive integer N and will calculate and return the product of the odd integers from 1 to N. Your function call should look like this : P = prodby2_username(N)
Find: How to write a function to fulfill the above parameters.
Issue: I can get the code to run, just not with the function call including the (N) input argument...
My Solution: I can get it to work in this configuration, with the call function of P = prodby2_kweave19 but not sure what to do about N...
function product = prodby2_kweave19(~) % Matlab recommended ~ since I don't have N in my function, *yet
% prodby2_kweave19 will produce product from 1 to N
% prodby2_kweave19 will receive a value of a positive integer N and will
% calculate and return the product of the odd integers from 1 to N.
product = prod(1:2:input('N= ')); % User inputs N, prod multiplies by all odd integers
end
The above functions as it's intended to, e.g. after typing MY function call it will prompt the user for a number i.e. 7 will return 105 as P. I just am finding difficulty with incorporating N another way that satisfies the requirements and can be called with P = prodby2_kweave19

 채택된 답변

Matt J
Matt J 2024년 3월 6일
편집: Matt J 2024년 3월 6일

0 개 추천

prodby2_kweave19(6)
ans = 15
function product = prodby2_kweave19(N)
product = prod(1:2:N); % User inputs N, as the argument to the function
end

댓글 수: 4

Spaceman
Spaceman 2024년 3월 6일
편집: Spaceman 2024년 3월 6일
Eureka! So was I missing the point that the call function contains the number they are calling it with? I.e. 6 and not the actual input argument N?
Matt J
Matt J 2024년 3월 6일
Yes, N receives the value 6 or whatever other value the function is called with.
N = input('N= ');
prodby2_kweave19(N)
function product = prodby2_kweave19(N)
product = prod(1:2:N); % User inputs N, as the argument to the function
end
Spaceman
Spaceman 2024년 3월 6일
Thank you. MATLAB humbles me daily.

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2023b

태그

질문:

2024년 3월 6일

댓글:

2024년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by