Coefficients from a symbolic 'nested' function

7 ビュー (過去 30 日間)
FBBVC
FBBVC 2020 年 3 月 10 日
回答済み: Guru Mohanty 2020 年 3 月 13 日
Unfortunatly, I'm stuck. In a previous question, I asked about a possibility to do a comparison of coefficients within a symbolic function. The tricky part seems to be (at least I think it is) that the function consist of a initial function as I have documented below. In the end, Im trying to receive a system of equations from every comparison of coefficients.
This is the code so far:
% coeffs a_k, b_k
coeffs = [sym('a0') ,sym('a', [1 2]), sym('b', [1 2])];
% Output: [a0, a1, a2, b1, b2]
% sin cos parts of function
trigs = [cos([1 2]*sym('tau')), sin([1 2]*sym('tau'))];
% Output: [cos(tau), cos(2*tau), sin(tau), sin(2*tau)]
% combine
initFC = symfun(sum([1 trigs] .* coeffs), [coeffs sym('tau')]);
% Output: initFC(a0, a1, a2, b1, b2, tau) = a0 + b2*sin(2*tau) + a1*cos(tau) + b1*sin(tau) + a2*cos(2*tau)
So that is my final inital function. Let me put that in any equation, e.g.
% Equation
F([coeffs sym('tau')]) = initFC.^2 == 0;
% Output: F(a0, a1, a2, b1, b2, tau) = (a0 + b1*sin(tau) + a1*cos(tau) + b2*sin(2*tau) + a2*cos(2*tau))^2 == 0
I can subs() any of the symbolic variables, superb! After expanding(F), combine(F, 'sincos') I receive a term that consists only of trigonometric terms:
F = expand(F);
F = combine(F, 'sincos')
% Ouput: (a1^2*cos(2*tau))/2 + (a2^2*cos(4*tau))/2 - ...
Now here is the problem: I need to substract the coefficients of this equation for e.g. cos(tau), cos(2*tau), ... .
I did like someone recommended before:
sym Z
% trigs(1) = cos(tau)
F = subs(F, trigs(1), Z)
% Output:
Error using symfun/subsindex (line 157)
Indexing values must be positive integers, logicals, or symbolic variables.
I also tried collect:
collect(F, Z)
% Output:
(2*a0*a1 + a1*a2 + b1*b2)*Z + (a1^2*cos(2*tau))/2 + ...
which is almost the solution which I'm seeking but there are still remaining parts of the equation within the output that I cant remove.
The final system of equations should look like this:
[2*a0*a1 + a1*a2 + b1*b2;
... next row from another comparison of coefficient;
...;
...]

回答 (1 件)

Guru Mohanty
Guru Mohanty 2020 年 3 月 13 日
Hi, I understand you are getting issues in removing all the coefficients from a given equation. You can do this by doing following adjustments to your code-
  1. Create an array of coefficients need to be replaced using syms function.
  2. Through an iteration substitute all the coefficients using subs function.
  3. Collect required expression through collect function.
Here is a sample code for it.
clc;
clear all;
% coeffs a_k, b_k
syms a0
syms a [1,2]
syms b [1,2]
coeffs = [a0 ,a1, a2, b1, b2];
% sin cos parts of function
trigs = [cos([1 2]*sym('tau')), sin([1 2]*sym('tau'))];
% combine
initFC = symfun(sum([1 trigs] .* coeffs), [coeffs sym('tau')]);
% Equation
F([coeffs sym('tau')]) = initFC.^2 == 0;
F = expand(F);
F = combine(F, 'sincos');
syms Z [8,1]
syms tau
%Coefficients to be replaced
coff_eqn=[cos([1 2 3 4]*tau), sin([1 2 3 4]*tau)];
for i=1:length(coff_eqn)
F = subs(F, coff_eqn(i), Z(i));
end
expr=collect(F,[Z(1:7)]);
display(expr);
espr=children(expr); % Find LHS of the expression
final=children(espr{1});
for i=1:length(coff_eqn)
Out(i)=final{i};
Out(i)=subs(Out(i), Z(i), 1);
end
display(Out);
The output is

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by