Plot a function - automatically label based on function input and output

조회 수: 1 (최근 30일)
Michael Jacobson
Michael Jacobson 2021년 3월 10일
댓글: Jan 2021년 3월 11일
I'm making an algorithm that can determine the root(s) of a provided function through various methods. The first method simply plots the function and lets the user see where the plot crosses the x-axis. The other method is modified regular false position, but that's not important.
Problem:
I am trying to have this algorithm plot the function. I want the x/y-axes labels to be automatic. That is, I want the algorithm to assess the function, determine what the input term and the output term are. I dont want to have to manually do this, because for every function these terms will be different. To help get a better understanding of what i've said, please see the following pictures.
This is a sample function. Note the input term: x and output term: r
Representation of desired result. Note that the x-axis is labeled 'x', the input term. Note that the y is labeled 'r', the output term.
Current plotting code. Note that i have manually entered the labels.
Thanks
  댓글 수: 3
Steven Lord
Steven Lord 2021년 3월 11일
Jan: In the posted code, the output is "random", not "r". "r" is the name of the function.
MATLAB knows this function as random because it is defined in the file random.m. The Note in the "Syntax for Function Definition" section on this documentation page states "To avoid confusion, use the same name for both the function file and the first function within the file. MATLAB associates your program with the file name, not the function name. Script files cannot have the same name as a function in the file."
Note the (somewhat hard to see) orange wavy line under r and the orange highlighting of r? That's Code Analyzer flagging that the file and function name are different.
To Michael Jacobson:
I share Jan's concerns. The names of the variables to which the function stores its input arguments internally are its business (and I suppose the business of the person who wrote the code.)
You're also going to encounter difficulties when the function uses something other than a normal variable in its definition. What's the name of the second input argument to this function?
function y = myfun(varargin)
How about the first input of this one?
function z = myodefun(~, y)
Rather than trying to access the internal names the author of the function whose handle you received, I'd consider using a name more descriptive of the role of that input like dependent and independent variables. Or you could just assume that the dependent variable is x and the independent is y and likely you'll be right often enough.
Jan
Jan 2021년 3월 11일
@Steven Lord: Thanks, now I see it: the file is called "random.m".

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by