Clear Filters
Clear Filters

i dont know how to use sum in a for loop

3 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 26 Dec 2021
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
S1 = 
X = 
W = 
273.8
S1 = 
X = 
W = 
38.43
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 Comments
Walter Roberson
Walter Roberson on 26 Dec 2021
double(X)
How do you want to work with L? Do you want length(L) by length(alpha) different outputs?
arian hoseini
arian hoseini on 26 Dec 2021
no.the last output is X=312 for L=8 but i want 2 or 3 more different X so i have to change L but i dont want to do it handly...sorry i hope you get what i mean

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!