Solving a function handle using solve or fsolve. How do I avoid error like 'Undefined function or variable' ?

2 次查看(过去 30 天)
Hello, I am trying to solve composite pulse problem using function handle and get the values of phi_0,phi_1,phi_2,phi_3,phi_4 and O by solving six equations after differentiation.
phi_5= 0;
a = @(O)cos(O/2);
b = @(O)-1i*sin(O/2);
ai = @(O)a(O);
bi = @(O)-b(O);
U0 = @(O,phi_0)[a(O),b(O)*exp(-1i*phi_0); -bi(O)*exp(1i*phi_0),ai(O)];
U1 = @(O,phi_1)[a(O),b(O)*exp(-1i*phi_1); -bi(O)*exp(1i*phi_1),ai(O)];
U2 = @(O,phi_2)[a(O),b(O)*exp(-1i*phi_2); -bi(O)*exp(1i*phi_2),ai(O)];
U3 = @(O,phi_3)[a(O),b(O)*exp(-1i*phi_3); -bi(O)*exp(1i*phi_3),ai(O)];
U4 = @(O,phi_4)[a(O),b(O)*exp(-1i*phi_4); -bi(O)*exp(1i*phi_4),ai(O)];
U5 = @(O)[a(O),b(O)*exp(-1i*phi_5); -bi(O)*exp(1i*phi_5),ai(O)];
U = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)U5(O)*U4(O,phi_4)*U3(O,phi_3)*U2(O,phi_2)*U1(O,phi_1)*U0(O,phi_0);
U_11 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)U(1,1);
U21 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)U(2,1);
U11 = inline('U_11','phi_0','phi_1','phi_2','phi_3','phi_4','O');
%u11 = symfun(U11,[O,phi_0,phi_1,phi_2]);
%u21 = symfun(U21,[O,phi_0,phi_1,phi_2]);
dU11_1 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,1);
dU11_2 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,2);
dU11_3 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,3);
dU11_4 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,4);
dU11_5 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,5);
dU11_6 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,6);
dU11_7 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,7);
dU11_8 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,8);
dU11_9 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,9);
U_eqn1 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_1;
U_eqn2 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_2;
U_eqn3 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_3;
U_eqn4 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_4;
U_eqn5 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_5;
U_eqn6 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_6;
U_eqn7 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_7;
U_eqn8 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_8;
U_eqn9 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_9;
U_eqn0 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)U_11;
%Phase
[O1,phiA,phiB,phiC,phiD,phiE] = solve(U_eqn1(phi_0,phi_1,phi_2,phi_3,phi_4,O),U_eqn3(phi_0,phi_1,phi_2,phi_3,phi_4,O),U_eqn5(phi_0,phi_1,phi_2,phi_3,phi_4,O),U_eqn7(phi_0,phi_1,phi_2,phi_3,phi_4,O),U_eqn9(phi_0,phi_1,phi_2,phi_3,phi_4,O),U_eqn0(phi_0,phi_1,phi_2,phi_3,phi_4,O),phi_0,phi_1,phi_2,phi_3,phi_4,O);
I want to solve the six equations ( in the last line of the code)using either solve or fsolve command? but I am getting this error.
Undefined function or variable 'phi_0'

回答(1 个)

Walter Roberson
Walter Roberson 2016-3-21
Do not use function handle notation for symbolic functions or symbolic expressions. You can differentiate a symbolic expression and you can differentiate a symbolic function, but you probably cannot differentiate a function handle.
I particularly recommend against using inline() with symbolic expressions.
I would also certainly recommend against using O as a variable name: it is much too easily confused with 0 .
Remember that when you have
dU11_1 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)diff(sym(U11),O,1);
U_eqn1 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O)dU11_1;
then you are saying that U_eqn1 should be a function that takes 6 variables, ignores them, and returns the function handle dU11_1 . Naming a function handle returns the function handle. If you want the function to be applied to the variables you need to invoke the function handle,
U_eqn1 = @(phi_0,phi_1,phi_2,phi_3,phi_4,O) dU11_1(phi_0,phi_1,phi_2,phi_3,phi_4,O)
... and if you are going to do that then why bother creating a new function handle? Why not at most use
U_eqn1 = dU11_1
which would copy the function handle.

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by