Info
This question is locked. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
trying to call a function from a function file in a script file
조회 수: 3 (최근 30일)
이전 댓글 표시
func = afunction; %trying to make the variable func equivalent to "afunciton",
% the function I created in the afunction file but it doesn't work :(
llim = -5;
ulim = -1;
options = optimset('Display','iter','TolX',10^-7,'Maxiter',500);
[x] = fzero(@func, [llim ulim],options);
I have a seperate function file called afunction that has the function afunction (which just has like a f(x)= ... function in it), but when I try to run this code the error says "Execution of script afunction as a function is not supported:
/Users/my name/Documents/MATLAB/afunction.m" in line 2
댓글 수: 2
Stephen23
2024년 7월 23일
편집: Stephen23
2024년 7월 23일
"but when I try to run this code the error says "Execution of script afunction as a function is not supported"
Get rid of all code outside of the function definition. Having code outside of the function definition makes it into a script, which will not work. You need to provide FZERO with a function handle, not run a script or call a function:
Do not call your function. Provide FZERO with a function handle:
x = fzero(@afunction, [llim ulim],options);
답변 (2개)
Muskan
2024년 7월 23일
편집: Muskan
2024년 7월 23일
Hi,
As per my undersatnding the issue is because you named the script "afunction"
Hence when when the script gets to this line:
func = afunction;
As a result, it tries to call "afunction" but cannot, because that is what you called your script.
The way to resolve this issue is to change the name of your script to something else.
You can refer to the following MATLAB Answer for more information: https://www.mathworks.com/matlabcentral/answers/1633265-i-have-this-error-execution-of-script-plot-as-a-function-is-not-supported-c-users-jonathan-e-le
댓글 수: 3
Stephen23
2024년 7월 23일
Get rid of all code outside of the function definition! This you should save in an MFILE of the same name:
function y = afunciton(x)
y = exp(cos(x)) - x.^3 + 2*x.^2 + 5*x - 3;
end
And this is how you call it:
llim = -5;
ulim = -1;
options = optimset('Display','iter','TolX',10^-7,'Maxiter',500);
x = fzero(@afunciton, [llim ulim],options)
Walter Roberson
2024년 7월 23일
func = @afunction; %trying to make the variable func equivalent to "afunciton",
% the function I created in the afunction file but it doesn't work :(
llim = -5;
ulim = -1;
options = optimset('Display','iter','TolX',10^-7,'Maxiter',500);
[x] = fzero(func, [llim ulim],options);
However... your afunction is a script rather than a function, and for the purposes of fzero you strictly need to use a function.
My guess is that your afunction.m starts something like
clear all; clc
function z = afunction(x)
That "clear all" at the beginning of it is unnecessary, and makes the code into a script file instead of a function file.
댓글 수: 0
This question is locked.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!