Undefined function 'equation' for input arguments of type 'double'

I want to write a function equation(M,epsilon,tol) which sets the solution of with an approximation error . So I have:
function x=newtonp(f, x, tol, h)
if nargin<4
h=1e-8
end
if nargin<3
tol=1e-8
end
while abs(f(x))>tol
g=(f(x+h)-f(x))/h
x=x-f(x)/g
end
end
function y=equation(M,epsilon,tol)
y=M+epsilon*sin(x)-x
end
Then I write:
newtonp(@equation(x),x, tol, h)
However I get
Undefined function 'equation' for input arguments of type 'double'
Can anyone correct my code?

10 件のコメント

per isakson
per isakson 2020 年 4 月 8 日
Is the function, equation(), stored in a separate mfile?
xtz xtz
xtz xtz 2020 年 4 月 8 日
I have this task at https://grader.mathworks.com/ so I have only box "function" and "code to call your function", I do not have the possibility of saving function in mfiles
per isakson
per isakson 2020 年 4 月 8 日
Caveat: I'm speculating, because I haven't used Grader.
You can probably only define one function. You can definately not reach a local function from "call your function".
I guess, you could use an anonymous function, which you define inside your single function. However, are you supposed to know the use of anonymous function?
xtz xtz
xtz xtz 2020 年 4 月 8 日
Yes, I have anonymous function on the lecture so I can use it but unfortunately I don't feel very confident in it and I don't know how to do it in this case...
per isakson
per isakson 2020 年 4 月 8 日
Inside your function, M and epsilon are constants
M = 1;
epsilon = 1e-6;
foo = @(x) M+epsilon*sin(x)-x;
foo(pi/3)
ans =
-0.047197
foo(0)
ans =
1
And use foo in your while-loop
xtz xtz
xtz xtz 2020 年 4 月 8 日
編集済み: xtz xtz 2020 年 4 月 8 日
M and epsilon are not costans: and it is checked in tests. For example one of them:
Test Code:
% Run learner solution;
tol = 1e-5;
y = equation(0.5,0.5,tol);
% Run reference solution.
yref = reference.equation(0.5,0.5,tol);
% Compare.
assessVariableEqual('y',yref,'AbsoluteTolerance',2*tol);
xtz xtz
xtz xtz 2020 年 4 月 8 日
The problem is that in test I have only three variable: (M,epsilon,tol). However when I use
equation=@(M,epsilon,tol) M+epsilon*sin(x)-x I get:
equation =
function_handle with value:
@(M,epsilon,tol,x)M+epsilon*sin(x)-x
Not enough input arguments.
because I didn't define x
per isakson
per isakson 2020 年 4 月 8 日
foo = @(M,epsilon,x) M+epsilon*sin(x)-x;
foo(0,0,0)
ans =
0
foo(0,1e-6,pi/100)
ans =
-0.031416
I suppose the task is to write one function called equation, which takes three inputs and returns one output.
per isakson
per isakson 2020 年 4 月 8 日
編集済み: per isakson 2020 年 4 月 8 日
Is this the first time you use Grader?
Why did you write the function called newtonp ?
xtz xtz
xtz xtz 2020 年 4 月 8 日
Yes, it is my first task in Grader. I wrote newtonp because in this task I can't just calculate this equation. My task is to use modified Newton's method to find a sufficient approximation of this function. Therefore, the equation function depends on tol not on x

サインインしてコメントする。

回答 (1 件)

per isakson
per isakson 2020 年 4 月 8 日
編集済み: per isakson 2020 年 4 月 8 日

0 投票

This is my guess
function x = equation( M, epsilon, tol )
h = 1e-8;
x = pi/6; % first guess
foo = @(x) M+epsilon*sin(x)-x;
while abs(foo(x))>tol
g=(foo(x+h)-foo(x))/h;
x=x-foo(x)/g;
end
end
Try it
y = equation(0.5,0.5,1e-8)
y =
0.88786
And why the second input is called epsilon is a mystery to me!

4 件のコメント

xtz xtz
xtz xtz 2020 年 4 月 8 日
Oh, great! Finally, my program passed the first test! Thank you very much now, but could you help me finish this task? This time this code was rated on 60%. I have information that:
  1. Pretest test - OK
  2. Task conditions - OK
  3. Solution accuracy test - OK
  4. Solution accuracy megatest - "Variable count has an incorrect value."
Do you have any idea what else might be wrong?
per isakson
per isakson 2020 年 4 月 8 日
No, isn't "accuracy megatest" mentioned in the task description? Does the test code give any hint?
xtz xtz
xtz xtz 2020 年 4 月 8 日
No, I have oppurtunity to see only pretest test and I have already shown it:
% Run learner solution;
tol = 1e-5;
y = equation(0.5,0.5,tol);
% Run reference solution.
yref = reference.equation(0.5,0.5,tol);
% Compare.
assessVariableEqual('y',yref,'AbsoluteTolerance',2*tol);
Meanwhile, I don't know anything about accuracy megatest.
As for the content of the task, it is its full form:
Write a function x=equation(M,epsilon,tol) which sets the solution of for some with an approximation error such that where a is an exact solution.
per isakson
per isakson 2020 年 4 月 9 日
編集済み: per isakson 2020 年 4 月 9 日
What's the meaning of "Variable count has an incorrect value." ? Could it refer to the number of variables in the function? No!
Speculation:
  1. Modify the sentence to "The variable, count, has an incorrect value." (It's not your English teacher who wrote that sentence.)
  2. There is a variable named count in the program that evaluates the submission
  3. count is used to keep track of the number of iterations
  4. implementations of numerical methods shall be efficient (among other requirements)
  5. "our" function, equation, might fail an efficiency test. Too many iterations. How did you chose the step value?
  6. Furthermore, in each iteration the function, foo, is evaluated three times for the same value of x. Maybe, count is the number of evaluations of foo().
Your turn!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeScripts についてさらに検索

製品

質問済み:

2020 年 4 月 8 日

編集済み:

2020 年 4 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by