trying to call a function from a function file in a script file (2024)

62 views (last 30 days)

Show older comments

Olivia on 23 Jul 2024 at 2:58

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file

  • Link

    Direct link to this question

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file

Edited: Olivia on 23 Jul 2024 at 4:12

This question was flagged by Stephen23

Show flagsHide flags

  • Flagged as Not appropriate for MATLAB Answers by Stephen23 on 23 Jul 2024 at 4:29.

    OP deleted the original question.

Open in MATLAB Online

func

1 Comment

Show -1 older commentsHide -1 older comments

Stephen23 on 23 Jul 2024 at 3:37

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218171

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218171

Edited: Stephen23 on 23 Jul 2024 at 3:57

Open in MATLAB Online

"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:

https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html

Do not call your function. Provide FZERO with a function handle:

x = fzero(@afunction, [llim ulim],options);

https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html

Sign in to comment.

Sign in to answer this question.

Answers (3)

Muskan on 23 Jul 2024 at 3:12

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#answer_1489081

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#answer_1489081

Edited: Muskan on 23 Jul 2024 at 3:45

Open in MATLAB Online

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 Comments

Show 1 older commentHide 1 older comment

Olivia on 23 Jul 2024 at 3:14

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218156

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218156

Edited: Olivia on 23 Jul 2024 at 3:21

i've tried renaming the function file to something else but then when I try to call afunciton it says function or variable not recognized :(

Muskan on 23 Jul 2024 at 3:17

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218161

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218161

Is it possible for you to share the script in order to help debug?

Stephen23 on 23 Jul 2024 at 3:51

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218181

  • Link

    Direct link to this comment

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#comment_3218181

Open in MATLAB Online

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)

Func-count x f(x) Procedure 2 -1 -3.28347 initial 3 -1.08663 -3.1958 interpolation 4 -1.08663 -3.1958 bisection 5 -1.28168 -2.68766 interpolation 6 -1.28168 -2.68766 interpolation 7 -1.53453 -1.31264 interpolation 8 -1.73932 0.461266 interpolation 9 -1.68607 -0.0601463 interpolation 10 -1.69221 -0.00220062 interpolation 11 -1.69244 7.41169e-07 interpolation 12 -1.69244 7.41169e-07 interpolation Zero found in the interval [-5, -1]

x = -1.6924

Sign in to comment.

Star Strider on 23 Jul 2024 at 3:17

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#answer_1489086

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#answer_1489086

Open in MATLAB Online

If ‘afunction’ is tthe same as in your previous questtion, then this syntax should work —

x = linspace(-6, 0, 250);

figure

plot(x, afunction(x))

grid

trying to call a function from a function file in a script file (8)

llim = -5;

ulim = -1;

options = optimset('Display','iter','TolX',10^-7,'Maxiter',500);

func = @(x) afunction(x)

func = function_handle with value:

@(x)afunction(x)

[x] = fzero(func, [llim ulim],options);

Error using fzero (line 287)
Function values at the interval endpoints must differ in sign.

function y = afunction(x)

y = exp(cos(x) - x.^3 + 2*x.^2 + 5*x - 3);

end

The problem is that it actually must cross 0 in the chosen interval to use fzero, and it obviously does not.

.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Walter Roberson on 23 Jul 2024 at 3:21

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#answer_1489091

  • Link

    Direct link to this answer

    https://matlabcentral.mathworks.com/matlabcentral/answers/2139466-trying-to-call-a-function-from-a-function-file-in-a-script-file#answer_1489091

Open in MATLAB Online

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 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABExternal Language InterfacesOther languagesR Language

Find more on R Language in Help Center and File Exchange

Tags

  • function call

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


trying to call a function from a function file in a script file (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

trying to call a function from a function file in a script file (2024)
Top Articles
Men's Moccasin Shops in Clarksville
Midwest Sound & Security
Kostner Wingback Bed
Custom Screensaver On The Non-touch Kindle 4
I Make $36,000 a Year, How Much House Can I Afford | SoFi
Busted Newspaper Zapata Tx
Craigslist Cars Augusta Ga
³µ¿Â«»ÍÀÇ Ã¢½ÃÀÚ À̸¸±¸ ¸íÀÎ, ¹Ì±¹ Ķ¸®Æ÷´Ï¾Æ ÁøÃâ - ¿ù°£ÆÄ¿öÄÚ¸®¾Æ
25X11X10 Atv Tires Tractor Supply
Alpha Kenny Buddy - Songs, Events and Music Stats | Viberate.com
His Lost Lycan Luna Chapter 5
Victoria Secret Comenity Easy Pay
Corporate Homepage | Publix Super Markets
12 Best Craigslist Apps for Android and iOS (2024)
Tripadvisor Near Me
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
Wunderground Huntington Beach
Ukraine-Russia war: Latest updates
Assets | HIVO Support
Shreveport Active 911
Uc Santa Cruz Events
Find Such That The Following Matrix Is Singular.
Army Oubs
Vegito Clothes Xenoverse 2
Amazing Lash Studio Casa Linda
Feathers
Cable Cove Whale Watching
Meijer Deli Trays Brochure
Tracking every 2024 Trade Deadline deal
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
Gridwords Factoring 1 Answers Pdf
Chadrad Swap Shop
Citibank Branch Locations In Orlando Florida
Minecraft Jar Google Drive
Selfservice Bright Lending
67-72 Chevy Truck Parts Craigslist
Magicseaweed Capitola
Babylon 2022 Showtimes Near Cinemark Downey And Xd
Stafford Rotoworld
2007 Peterbilt 387 Fuse Box Diagram
2700 Yen To Usd
888-822-3743
Thor Majestic 23A Floor Plan
Alston – Travel guide at Wikivoyage
Saline Inmate Roster
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
War Room Pandemic Rumble
Xre 00251
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
Barber Gym Quantico Hours
Campaign Blacksmith Bench
Kenmore Coldspot Model 106 Light Bulb Replacement
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5423

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.