Problem with answer 15.2 Creating and Calling Functions: (4/6) Create and Call a Function, task 1 MATLAB Fundamentals

The problem asks us to modify the function call using two inputs. I put the answer as follows:
top5 = getLargestN(x,5)
i get a message 'error using exercise>getLargestN too many input arguments'.
The answer i solution is identical to one I gave. How do I move forward here?

 채택된 답변

Gareth - did you update the function signature as well so that it accepts two inputs? Presumably you have defined the function (with body) like
function [res] = getLargestN(x)
and you would need to change this to
function [res] = getLargestN(x,y)
% do something in code with y
so that the signature matches how you are calling it (when you pass in the x and 5).

댓글 수: 3

Hi Geoff, thanks for reaching out. I removed the 'function' in the version I sent becuase that wasn't working either. The task is just to modify the function call in this instance. So I'm running exactly this:
function Y= getLargestN(x,5)
top5=Y
end
the task is here:
  1. Modify the script so that the function takes two inputs. The second input should represent the number of the largest elements to return. For example,t = getLargestN(v,5)should output a vector containing the five largest elements of v.
  2. Modify the function call in the script and store the five largest elements in a vector named top5.
my result here: Test Results:
Incorrect!
Check that x is not modified. X
Does the function evaluate correctly? X
no idea how i'm modifying x here
Gareth - I would first try to get the function returning the correct response (I don't understand either what "check that x is not modified" means). So right now your function is
function Y= getLargestN(x,5)
top5=Y
end
There are a couple of problems. The signature defines the output parameter and the input parameters. These parameters are variables and since 5 is not a variable (but a value) you cannot define it as a parameter. (You can pass 5 into this function when you call it.) So change this to something like
function Y = getLargestN(x, numElements)
Try to name the variables as to their purpose. Next, the output parameter is Y but is not set anywhere in your code. In fact, you are trying to use it before it has been defined. (I think the top5 variable is meant to be the output from this function when you call it...and not a variable within the function because you don't know how many elements you will be asked to extract.
function Y = getLargestN(x, numElements)
Y = ...;
end
Now you just have to sort the data.
Thanks Geoff! I got there in the end. Really helpful. I've been used to doing these tasks one at a time, so didn't anticipate having to edit code in a window further down first :)

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

추가 답변 (5개)

Sanket
Sanket 2022년 11월 4일
y0 = 0.4;
yline(y0)
tzerox = findcrossing(t,x-y0)
tzeroy = findcrossing(t,y-y0)
y0 = 0.4;
yline(y0)
tzerox = findcrossing(t,x,y0)
tzeroy = findcrossing(t,y,y0)

댓글 수: 5

The real solution is different than the solution which the course gives.
y0 = 0.4;
yline(y0)
tzerox = findcrossing(t,x-y0)
tzeroy = findcrossing(t,y-y0)
Thank you, the code helped me.!

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

  1. Modify the definition of the findcrossing function so that it takes a third input z.
  2. Add a new line to the beginning of the function:y = y - z;
  3. In the Task 1 section of the script, change the value of y0 to 0.4.
  4. Modify the two calls to findcrossing to add y0 as an input.
You can use the graph to check that the returned values of t are correct (x(t) = 0.4 and y(t) = 0.4)
Then modify the command for Task 1 so that r has a value of 0.5.

카테고리

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

질문:

2020년 5월 4일

답변:

2024년 12월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by