Hi
c0 must be numeric, but my symbolic functions can not work correctly?
Where is my mistake?
Thx
clear all
clc
syms x1 x2
F=x1^2+x2^2-4*x1-2*x1*x2;
gradx1=gradient(F,x1);
gradx2=gradient(F,x2);
c=[gradx1; gradx2];
fun_x0=@(x1,x2) F;
fun_c1_x0=@(x1,x2) gradx1;
fun_c2_x0=@(x1,x2) gradx2;
x0=[1,1];
c_x0_1=fun_c1_x0(x0(1),x0(2));
c_x0_2= fun_c2_x0(x0(1),x0(2));
c0=[c_x0_1; c_x0_2];
c0=c0';

 Accepted Answer

madhan ravi
madhan ravi on 22 Dec 2018
Edited: madhan ravi on 22 Dec 2018
Because you mixed function handle in place of subs()/matlabFunction().
Try this:
syms x1 x2
F=x1^2+x2^2-4*x1-2*x1*x2;
gradx1=gradient(F,x1);
gradx2=gradient(F,x2);
c=[gradx1; gradx2];
fun_x0= F;
fun_c1_x0=gradx1
fun_c2_x0=gradx2
x0=[1,1];
c_x0_1=subs(fun_c1_x0,{x1,x2},{x0(1),x0(2)});
c_x0_2=subs(fun_c2_x0,{x1,x2},{x0(1),x0(2)});
c0=[c_x0_1; c_x0_2];
c0=c0.'

2 Comments

Anytime :) , here is another simple way:
syms x1 x2
F=x1^2+x2^2-4*x1-2*x1*x2;
gradx1=gradient(F,x1);
gradx2=gradient(F,x2);
c=[gradx1; gradx2];
fun_x0= F;
fun_c1_x0(x1,x2)=gradx1;
fun_c2_x0(x1,x2)=gradx2;
x0=[1,1];
c_x0_1=fun_c1_x0(x0(1),x0(2));
c_x0_2= fun_c2_x0(x0(1),x0(2));
c0=[c_x0_1; c_x0_2];
c0=c0.'

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB 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!