Hi, I am just doing some homework after spring break so I forgot some important things about matlab saving sript.
This is my homework: Write a function called odd_rms that returns orms, the square root of the mean of the squares of the first n positive odd integers, where n is a positive integer and the only input argument.
So far, I have tried to do it and I am actually know how to write a code for this:
This is my try :
function orms=odd_rms(n)
A=sqrt(mean((1:2:2n-1).^2));
end
I save my script as odd_rms and try it on command window something like odd_rms(3) but it doesn't show any answer (Error: File: odd_rms.m Line: 2 Column: 19)
I have tried plugging random number to it specific code sqrt(mean((1:2:2n-1).^2)) and they return the right answer.
Can someone explain the error on my script?
Thanks you so much

 채택된 답변

Birdman
Birdman 2020년 3월 24일
편집: Birdman 2020년 3월 24일

0 개 추천

You forgot to put multiplication operator. Try this:
function A=odd_rms(n)
A=sqrt(mean((1:2:2*n-1).^2));
end

댓글 수: 4

David
David 2020년 3월 24일
thanks you. That really help me
Hi, this morning i try it and it was succesful, in the afternoon I tried again and it say : (Function 'odd_rms' has already been declared within this scope.)
This is my function :
function orms=odd_rms(n)
A=sqrt(mean((1:2:2*n-1).^2))
end
and I try it like : odd_rms(5)
I save the file as odd_rms
Walter Roberson
Walter Roberson 2020년 3월 24일
You accidentally included some commands before the function declaration, leading you to accidentally have the situation of having a script named odd_rms.m that includes a function named odd_rms . When you have a script, you cannot include a function with the same name as the script.
David
David 2020년 3월 24일
awesome, thanks you. I get it

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 3월 24일

0 개 추천

Your function has variable orms as output but you only assign to A.

댓글 수: 1

function orms=odd_rms(n)
orms=sqrt(mean((1:2:2*n-1).^2))
end
Is that correct?

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

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2020년 3월 24일

댓글:

2020년 3월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by