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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!