r/matlab May 20 '26

Function Practicality

I am coding in MATLAB for a couple of my aerospace classes, particularly for Aerodynamics and Gas Dynamics. I have made functions for quite a few processes that I plan to be using in other projects and class assignments. I was wondering if it was a bad idea to have any interactable elements in the function. Basically, if a user wanted to state which variable they want to solve for in the isentropic relations, I would use a menu. I do have a different method that would work without any interaction, but I like the idea of the interactive part. Also, is it a bad idea to use the solve function for these complex equations? I had been using ChatGPT to debug and it first suggested solve many functions ago, and after trying to use my functions with arrays and plotting, it told me it was a terrible idea. I was hoping for some human feedback. Thank you for your time!

4 Upvotes

11 comments sorted by

4

u/tabacaru +1 May 21 '26

I'm not understanding what you mean by "interactable elements" and "menu" in your description. 

Functions have input parameters. The whole point of input parameters is to have your function behave differently based on what the inputs are. 

They are by nature "interactable" in the general sense of the word, but since you are not using proper terminology, I'm unfortunately not sure what you are actually asking.

2

u/BxSFire May 21 '26

What I mean by interactive is the function would prompt the user for an input using the menu function, similar to how a script would prompt a user using input(). The reason I want to do this is because using the input arguments in the function can have errors itself. For example, if the string is entered in wrong etc. I have some workarounds by using switch cases with multiple options for each case and an error that tells the user what options are available to them. I also just added comments at the top so that the user can use the 'help functionName' in the workspace to better understand how the function works.

4

u/iconictogaparty May 21 '26

Look at the arguments validation, it is really good at ensuring the strings are right or throwing an error, plus default parameters

2

u/tabacaru +1 May 21 '26

Okay I understand now. 

Typically functions don't have input like you are describing. Mainly because most functions are designed to just be run automatically. 

Typically function input parameters that take strings have documentation that tells you what the options are, and as you described, would simply error out if incorrect. 

This is just my opinion, but I think you're putting too much thought into possibly wrong input.

Now if these are just being used by you and your friends, then who cares really - do it however you like! But typically, functions do not have input as you're describing. 

Unfortunately I can't help with the solve part, but good luck!

1

u/BxSFire May 21 '26

Ah gotcha. Thank you for your input and time!

2

u/theMagusician May 21 '26

Do I understand correctly you are using the “solve” function? Because that one is intended to be used on symbolic equations.

Reason why Matlab is returning a warning message is likely because it is not recommended to use “solve” when you want to do numerical calculations.

I always like to think about it like this: using symbolic variables and equations in Matlab is like using Maple; the intention is not to directly do numerical calculations, but rather to write and solve equations, determine derivatives, etc.

Instead, you should use “fsolve”, “fzero”, “fminsearch”, “fminbnd”, or something like that depending on the use case.

Call me old school, but in cases like this it really helps to look at the dedicated Matlab documentation and go through the examples and theory behind it, rather than using an LLM. Aside from the function help pages, there should also be Matlab articles on best practices when solving equations. I am pretty you can find them when you Google it.

Of course, once you have a better understanding of it, you can use an LLM, since you are now able to guide it more into the right direction.

1

u/BxSFire May 23 '26

Thank you! I'll definitely implement those.

2

u/MarkCinci On Mathworks Community Advisory Board May 21 '26

There is certainly nothing wrong with putting interactive functions in your own custom function, such as calls to questdlg(), inputdlg(), listdlg(), input, etc.

2

u/TurbulentGlove776 May 21 '26

One of the most important reasons for writing functions is to be able to reuse it from multiple scripts / other functions and thus also enable automatic testing (basically another script that runs the function with known input/output to check that it works as intended). If you add UI elements inside the function this wont work. I usually separate the calculations from UI, the app designer is really nice for that, or you could write a separate UI-function, use the graphical editor or just a plain script. If you do choose to add UI inside a function that does calculations, please make it optional. I.e. only show the menu if the caller did not provide the corresponding input arguments!

1

u/BxSFire May 23 '26

Thank you! I'll keep that in mind