i dont know how to use sum in a for loop
2 次查看(过去 30 天)
显示 更早的评论
clc
clear all
% -------------------------------------data input-----------------------------------
alpha = [510; 310; 78; 120; 350; 490; 53; 150; 170; 149];
beta = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
gamma = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
delta = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
% Pmin =[150; 100; 50; 50; 100; 100; 100; 100; 100; 100];
% Pmax =[600; 400; 200; 200; 350; 500; 300; 300; 300; 300];
n = 10;
PD = 2500;
L = 8;
%--------------------------------------Program start here--------------------------------
syms P
% P = sym('P');
% L = sym('L');
F = sym('F');
Lambda = sym('Lambda');
% n = length(alpha);
for i = 1:2
F(i) = alpha(i) + beta(i).*P + gamma(i).*P.^2 + delta(i).*P.^3;
disp(F(i))
Lambda = diff(F(i),P) - L == 0;
disp(Lambda);
S = solve(Lambda, P);
disp(S)
S1 = max(S)
% disp(S1)
X(i) = sum(S1)
W = vpa(X(i),4)
end
The answer of X(i) must be 312.2300 but its not...so what should i do exactly?
0 个评论
采纳的回答
Walter Roberson
2021-12-26
clc
clear all
% -------------------------------------data input-----------------------------------
alpha = [510; 310; 78; 120; 350; 490; 53; 150; 170; 149];
beta = [7.20; 7.85; 7.98; 7.10; 7.30; 6.90; 8.01; 7.30; 7.42; 7.16];
gamma = [0.00142; 0.00194; 0.00482; 0.00321; 0.00210; 0.00178; 0.00212; 0.00350; 0.00267; 0.00390];
delta = [0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001; 0.0000002; 0.0000003; 0.0000001];
% Pmin =[150; 100; 50; 50; 100; 100; 100; 100; 100; 100];
% Pmax =[600; 400; 200; 200; 350; 500; 300; 300; 300; 300];
n = 10;
PD = 2500;
L = 8;
%--------------------------------------Program start here--------------------------------
syms P
% P = sym('P');
% L = sym('L');
F = sym('F');
Lambda = sym('Lambda');
% n = length(alpha);
for i = 1:2
F(i) = alpha(i) + beta(i).*P + gamma(i).*P.^2 + delta(i).*P.^3;
disp(F(i))
Lambda = diff(F(i),P) - L == 0;
disp(Lambda);
S = solve(Lambda, P);
disp(S)
S1 = max(S)
% disp(S1)
X(i) = sum(S1)
W = vpa(X(i),4)
end
You can see from this output that each S result (the roots of the polynomial) has two elements. S1 is then max() of that vector of two elements, so S1 is scalar. Then you ask to sum() the scalar; the result is going to be the same as the scalar.
It is not clear whether you want one single X value that is a total of the S1 values, or if you want a cumulative sum, [first S1, first S1 + second S1, first S1 + second S1 + third S1 and so on] . If you want just a single total the
X(i) = S1;
and after the loop
total_X = sum(X);
If you want a cumulative sum, then after the loop,
cumtotal_X = cumsum(X);
3 个评论
Walter Roberson
2021-12-26
double(X)
How do you want to work with L? Do you want length(L) by length(alpha) different outputs?
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!