The desired expression can be evaluated in two ways. One of them uses symbolic math while the other avoids symbolic math altogether. Both these approaches use the data stored in the table. Have a look at the modified code below:
syms P_lv(t) C1 C2 C3 C4 R0 R1 R2 R3 R4 Rb Rp Rl Rr L1 L2 L3 L4 R_lv t
syms dens l h d E u
[C1, C2, C3, C4] = deal(6.158 * 10^10);
[L1, L2, L3, L4] = deal(344294);
[R1, R2, R3, R4] = deal(2.651 * 10^12);
[Rb, Rr, Rp, Rl] = deal(2.2 * 10^9);
%P_lv(t) = 1/0.0075 * (a0 + synsum(am*sin(2*pi*fm*t+phi_m),m,1,10));
table_A = table([0;1;2;3;4;5;6;7;8;9;10],...
[80;32.1;16;10.5;5;2.6;3.9;2.4;1;1.8;1.5],...
[0;-3.11;-1.25;0.71;2.8;-2.44;-0.84;1.38;2.7;-2.22;-0.14],...
'Variablenames',{'fm','am','phi_m'});
f = @testy;
t = 0; % set t here
% Approach which uses symbolic math
output_B = rowfun(f,table_A(2:end,:),'SeparateInputs',false,'OutputFormat','uniform');
P_lv = double(sum(subs(output_B,t)));
% Approach which doesn't use symbolic math
P_lv2 = testy2(table_A,t);
function y = testy(vals,t)
syms am fm t phi_m
a0 = 80/10;
exprn = 1/0.0075 * (a0 + am*sin(2*pi*fm*t+phi_m));
y = subs(exprn, [fm,am,phi_m],vals);
end
function y = testy2(T,t)
a0 = T{1,2};
T = T(2:end,:);
y = 2*pi*T.fm*t+T.phi_m;
y = T.am.*sin(y);
y = 1/0.0075*(a0+sum(y));
end
The significant changes are:
- Removed the ‘m’ variable from table_A.
- rowfun is used to evaluate the user-defined function testy at every row of table_A.
- The symbolic expression for each row is evaluated at t and all the values are then summed up. The value of a0 has been appropriately modified.
- The user-defined function testy2, on the other hand, computes the value of the desired expression for a value t without using any symbolic math.
The ‘SeparateInputs’ parameter in the rowfun function ensures that each row is passed as a vector to the specified function. The ‘OutputFormat’ parameter ensures that the output is of class sym instead of a table.
For more details have a look at: