please help me check the error

1 visualización (últimos 30 días)
David
David el 24 de Mzo. de 2020
Comentada: David el 24 de Mzo. de 2020
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

Respuesta aceptada

Birdman
Birdman el 24 de Mzo. de 2020
Editada: Birdman el 24 de Mzo. de 2020
You forgot to put multiplication operator. Try this:
function A=odd_rms(n)
A=sqrt(mean((1:2:2*n-1).^2));
end
  4 comentarios
Walter Roberson
Walter Roberson el 24 de Mzo. de 2020
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 el 24 de Mzo. de 2020
awesome, thanks you. I get it

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 24 de Mzo. de 2020
Your function has variable orms as output but you only assign to A.
  1 comentario
David
David el 24 de Mzo. de 2020
function orms=odd_rms(n)
orms=sqrt(mean((1:2:2*n-1).^2))
end
Is that correct?

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by