how to evaluate a symbolic function in matlab
Mostrar comentarios más antiguos
Hi,
I am trying to automate my code to get the derivative of a function an evaluate that in given points. For example,
syms x
func=cos((pi*x)^4);
RHS=diff(func,x,2);
% RHS will be ==> - 16*pi^8*x^6*cos(pi^4*x^4) - 12*pi^4*x^2*sin(pi^4*x^4)
x=[ -1
-0.978147600733806
-0.913545457642601
-0.809016994374947
-0.669130606358858
-0.5
-0.309016994374947
-0.104528463267653
0.104528463267653
0.309016994374947
0.5
0.669130606358858
0.809016994374947
0.913545457642601
0.978147600733806
1]
and I want RHS and Func in those z values as a vector.
I appreciate the help. Thank you.
Respuestas (4)
ChristianW
el 11 de Feb. de 2013
1 voto
- doc subs
- doc subexpr
Youssef Khmou
el 11 de Feb. de 2013
hi, you can use function_handle :
func=@(x) cos((pi*x).^4)
x=0:100; % example of vector x .
RHS=diff(func(x),2);
8 comentarios
Kamuran
el 12 de Feb. de 2013
Youssef Khmou
el 12 de Feb. de 2013
Editada: Youssef Khmou
el 12 de Feb. de 2013
Kamuran, that is normal routine for "diff" function , if you differentiate func with length p : diff(func,n) n times then the returned vector has p-n points .
if you put :
RHS4=diff(func(x),4);
Length of RHS4 is 101-4 .
diff produces a shift, you can truncate the 1st element of the RHS(1)=[];
Brian B
el 12 de Feb. de 2013
diff operates differently on symbolic objects than on numeric vectors. You want (and have, as shown in your question) an analytical expression for the second derivative. You just have to substitute the numerical values of x at which you want the solution. Did you look at the functions suggested above by ChristianW?
Youssef Khmou
el 12 de Feb. de 2013
The values are right Kamuran :
x=0:100;
func=@(x) x.^3;
f=x.^3;
df2=6.*x;
RHS=diff(func(x),2);
figure, plot(df2), hold on, plot(RHS,'r')
legend(' numerical d²f',' diff(function_handle,2)')
Youssef Khmou
el 12 de Feb. de 2013
Editada: Youssef Khmou
el 12 de Feb. de 2013
Brian : of course with linear x , what is your solution then? can you write a Diff function that gives the approximate der whatever the x is .
Youssef Khmou
el 12 de Feb. de 2013
Editada: Youssef Khmou
el 12 de Feb. de 2013
Well in this case you have to add diff(x) as DENOMINATOR :
Given your x :
func=@(x) x.^3;
f=x.^3;
df2=6.*x;
df2(1)=[];
d1=diff(func(x))./diff(x);
RHS=diff(d1)./diff(x(1:end-1));
figure, plot(df2), hold on, plot(RHS,'r')
legend(' numerical d²f',' diff(function_handle,2)')
you have to increase the Sample Rate in x to get better approximation .
Hi Youssef,
I'm not saying your solution is not valid. I was simply pointing out that the original question refers to symbolic calculation of an exact derivative. It is possible to evaluate the symbolic expression at any arbitrary set of points, without regard to interval size or ordering. That is what the subs command does, to which ChristianW referred. See my answer below.
regards,
Brian
Youssef Khmou
el 12 de Feb. de 2013
Editada: Youssef Khmou
el 12 de Feb. de 2013
Kamuran, to get better approximation you need to increase the sample rate in x and interpolate :
x=[ -1
-0.978147600733806
-0.913545457642601
-0.809016994374947
-0.669130606358858
-0.5
-0.309016994374947
-0.104528463267653
0.104528463267653
0.309016994374947
0.5
0.669130606358858
0.809016994374947
0.913545457642601
0.978147600733806
1];
x=x';
x=interp(x,5); % Example
func=@(x) x.^3;
f=x.^3;
df2=6.*x;
df2(1)=[];
d1=diff(func(x))./diff(x);
RHS=diff(d1)./diff(x(1:end-1));
figure, plot(df2), hold on, plot(RHS,'r')
legend(' numerical d²f',' diff(function_handle,2)')
Compare this result with the one given in the Comment with original x, there is an enhancement .
I hope that helps
Brian B
el 12 de Feb. de 2013
syms x
func=cos((pi*x)^4);
RHS=diff(func,x,2);
xx=-1:0.1:1;
d2f = subs(RHS, xx)
2 comentarios
Youssef Khmou
el 13 de Feb. de 2013
right , that works better with "subs" function .
vikas singh
el 12 de Mzo. de 2023
if I have function of two variable suppose x and t and I have to evaluate it on x=0:100:10000 and t= 0,1,5,10 15. how to do. suppose the fucnction is F(x,t)=sin(x)*exp(t)
Categorías
Más información sobre Calculus en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!