How to input equations in MATLAB and operate it?
Show older comments
I'm using MATLAB R2013a. I want to input an equation in Matlab like f(x) = x^2 - 2*x + 3. And I want it to be inputted by the user. I use inline() function for this. eg.
func = input('Enter a function: ','s');
f = inline(func);
And I have able to do some works with it. But I want to differentiate this equation. I was trying to use diff() function for this. But it doesn't work. When I use diff(f,x); it shows error. How can I solve my problem?
Accepted Answer
More Answers (2)
Jonathan Moussa NDAO
on 17 May 2021
You can try using the matlab builtin func str2func that convert your input in a matlab function handler like syntax('
')
func = str2func(input("Enter the function, e.g @(x)2*x - 3: ", "s"));
robert michael
on 22 Jun 2022
0 votes
f(x) = x3 – x – 1 input in matlab?
1 Comment
Walter Roberson
on 23 Jun 2022
f = @(x) x.^3 - x - 1
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!