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:
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
2020년 4월 8일
Is the function, equation(), stored in a separate mfile?
xtz xtz
2020년 4월 8일
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
2020년 4월 8일
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
2020년 4월 8일
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
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
2020년 4월 8일
답변 (1개)
per isakson
2020년 4월 8일
편집: per isakson
2020년 4월 8일
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
2020년 4월 8일
per isakson
2020년 4월 8일
No, isn't "accuracy megatest" mentioned in the task description? Does the test code give any hint?
xtz xtz
2020년 4월 8일
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:
- Modify the sentence to "The variable, count, has an incorrect value." (It's not your English teacher who wrote that sentence.)
- There is a variable named count in the program that evaluates the submission
- count is used to keep track of the number of iterations
- implementations of numerical methods shall be efficient (among other requirements)
- "our" function, equation, might fail an efficiency test. Too many iterations. How did you chose the step value?
- 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 Exchange에서 Scripts에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




