MATLAB Answers

0

How can I create series of equations to graph, that might have a different number of equations every time, without using dynamic variable naming?

Asked by Emily Eastman on 29 Aug 2017
Latest activity Edited by José-Luis
on 29 Aug 2017
I'm trying to write myself a nice Matlab program that lets me input arguments based on prompts and will graph the equations I put in. I need it to be able to graph anywhere from 1-10 equations at a time, but i cant figure out how to set them all up with names, without naming variables dynamically with something like eval. I know dynamic variable naming is bad practice, but I don't know of another way to do it.
(I am trying to improve my proficiency in mat lab beyond using it as a large graphing calculator, and I know the graphing commands so this lets me learn about script writing and know what I was trying to draw, so I can tell what went wrong)
I would also appreciate knowing if there are multiple methods to achieve this.

  2 Comments

Sign in to comment.

1 Answer

Answer by José-Luis
on 29 Aug 2017
Edited by José-Luis
on 29 Aug 2017

You can place function handles in cell arrays:
funArray = cell(2,1);
funArray{1} = @(x) x.^2;
funArray{2} = @(x) 3.*x + 5;
funArray{1}(3)
funArray{2}(3)
funArray{1}(1:5)
fplot(funArray{1},[-5,5])
In order to parse your inputs to a function handle, you could try using str2func() which is, allegedly, a step above eval().

  0 Comments

Sign in to comment.