how to calculate derivative of a string

7 views (last 30 days)
dear all
i have a problem with calculating the derivative of a aglebraic expression which is string. how can i use diff to calculate the derivative of expression?
the problem is as below:
eq='x^2+x';
f=str2func(['@(x)',eq]);
g=diff(f);
thanks alot

Accepted Answer

madhan ravi
madhan ravi on 1 Apr 2019
eq='x^2+x';
f = sym(str2func(['@(x)',eq]));
g = diff(f)
  5 Comments
Ashesh Choudhury
Ashesh Choudhury on 30 Sep 2020
Thank you so much. You are a saviour. @madhan

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 1 Apr 2019
Replace your str2func, by str2sym to create a symbolic expression:
eq='x^2+x';
f = str2sym(eq);
g = diff(f);
  1 Comment
saeed rezaei
saeed rezaei on 1 Apr 2019
thank you for your help
but, it seems that str2sym function doesn't work in matlab R2015a.
do you have any other suggestion ?

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox 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!