Currently writing a symfun with one input and three outputs. The equation is
exp(x) -3*x.^2 +1
. Input is x
and the outputs are the equation itself (denoted by f
), its first derivative (denoted by fp
) and the second derivative (denoted by fpp
). Trying to plot these three graphs on the interval [-5 5]
. I am using fplot(fun[-5 5]);
. The only graph shown is exp(x) -3*x.^2 +1
. Any help would be appreciated.
Asked
Active
Viewed 89 times
1

EBH
- 10,350
- 3
- 34
- 59

Matlab rookie
- 23
- 1
- 6
-
Could you post what you've tried using code block formatting to make it more clear what you're trying? – jodag Aug 20 '16 at 19:11
-
@jodag edited hope its clearer. – Matlab rookie Aug 20 '16 at 19:30
-
For plotting three graphs of three functions f1, f2, f3, use: `fplot(f1,[-5 5]);hold on;fplot(f2,[-5 5]);hold on;fplot(f3,[-5 5]);hold on;` – Rotem Aug 20 '16 at 21:38
1 Answers
1
Here is a code for that (no need for hold
):
syms x
f = symfun(exp(x) -3*x.^2 +1,x);
fp = diff(f);
fpp = diff(fp);
fplot([f,fp,fpp],[-5 5])
legend({char(f),char(fp),char(fpp)})
you need to put all functions in a vector (i.e. [f,fp,fpp]
).
The output:

EBH
- 10,350
- 3
- 34
- 59
-
Undefined function or variable. Trying to write it in a way that the plotting is in a script and everything up to and including the fpp is in a function so that I can use the function again later on. – Matlab rookie Aug 21 '16 at 09:02
-
When I run just this, without writing anything else I get the plot and no error, so try to be more specific about the error you get. Run this line by line, and see which line causing the problem – EBH Aug 21 '16 at 09:07
-
Currently using a function with the following text. sym x f= (exp(x) -3*x.^2 +1,x); fp= diff(f); fpp= diff(fp) This works perfectly. Then I have a script file containing the following fplot([f,fp,fpp],[-5 5] The error message that appears is undefined function or variable f error in script fplot([f,fp,fpp],[-5 5]) – Matlab rookie Aug 21 '16 at 09:20
-
If you want to use the functions `f`, `fp` and `fpp` inside another function then you need to pass it as input, you can't just have it in the workspace. This should be something like: `plot_ffpfpp (f,fp,fpp)` where `plot_ffpfpp` is your function name – EBH Aug 21 '16 at 09:30
-
So `plot_fun(f,fp,fpp)`? Is this put into the function or the script? – Matlab rookie Aug 21 '16 at 09:58
-
say you have a function`fun` that define `f,fp,fpp` and now you want to use this functions (`f,fp,fpp`) in a script outside `fun`, then you must pass it as output from `fun` so the first line in `fun` will be: `[f,fp,fpp] = fun (some input)`, then you can write in your script: `[f,fp,fpp] = fun (some input)`, and after this: `fplot([f,fp,fpp],[-5 5])`, and it will work – EBH Aug 21 '16 at 10:08