Diff () function
Diff () function
(OP)
I am a student, and we have put together a small script to calculate the roots of an equation using newton raphson itteration in Matlab - all well and good. However, I decided it would be nice if you could change the function from the terminal, rather than changing the code each time you wanted to solve a new problem.
The problem is, the way most people will enter an equation is in symbolic notation (e.g x^2 +3). On top of this, I need to differentiate my function, (i.e obtain 2x from the above). The diff function will do this, but only (it seems) if fed a symbolic equation.
However, I need the equation in the form
x.^2 +3 - note the x. for element operations - or else my program fails spectacularly
Is there any way I can convert from symbolic notation to this?
Thanks
Ben
The problem is, the way most people will enter an equation is in symbolic notation (e.g x^2 +3). On top of this, I need to differentiate my function, (i.e obtain 2x from the above). The diff function will do this, but only (it seems) if fed a symbolic equation.
However, I need the equation in the form
x.^2 +3 - note the x. for element operations - or else my program fails spectacularly
Is there any way I can convert from symbolic notation to this?
Thanks
Ben





RE: Diff () function
Just convert it to non-symbolic array:
>> X=sym(magic(4))
X = % symbolic input
[ 16, 2, 3, 13]
[ 5, 11, 10, 8]
[ 9, 7, 6, 12]
[ 4, 14, 15, 1]
>> z=double(X) % get ride of symbolic properties
z =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> d=diff(z.^2-1)
d =
-231 117 91 -105
56 -72 -64 80
-65 147 189 -143
D=sym(d) % If you need a symbolic result
D =
[ -231, 117, 91, -105]
[ 56, -72, -64, 80]
[ -65, 147, 189, -143]