Table values as input to a function

조회 수: 17 (최근 30일)
Per Nordeman
Per Nordeman 2019년 9월 26일
답변: Asvin Kumar 2019년 10월 1일
Hello!
Im trying to execute the following equation as shown in the image:
Having trouble using the values in the table as input to the fuction.
How should i do this? (quite new at matalb).
The current code is down belowSkärmavbild 2019-09-26 kl. 22.16.45.png
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
syms am fm t phi_m m
[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],...
[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',{'m','fm','am','phi_m'});
f = @testy;
output_B = varfun(f,table_A);
function y = testy(am, fm, t, phi_m, m)
y = 1/0.0075 * (80 + symsum(am*sin(2*pi*fm*t+phi_m),m,1,10));
end

답변 (1개)

Asvin Kumar
Asvin Kumar 2019년 10월 1일
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:
  1. Removed the ‘m’ variable from table_A.
  2. rowfun is used to evaluate the user-defined function testy at every row of table_A.
  3. 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.
  4. 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:

카테고리

Help CenterFile Exchange에서 Formula Manipulation and Simplification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by