I am out of practice with MATLAB and I'm trying to get the sum from a loop to add itself to the sum of each iteration of the loop. I can accomplish by manually stepping the loop, but I know there must be an easier way. Can someone please help?

21 visualizzazioni (ultimi 30 giorni)
%Manual stepping of "loop"
clear
clc
h=input('What is height? ');
l=input('What is length? ');
w=input('What is frequency? ');
y=input('How many iterations? ');
x=0:0.001:l;
t=0;
F=(8*h)/(pi)^2;
n=1;
SUM=0
for n=1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S4=F*(S1*S2*S3);
SUM1=S4;
figure
plot(x,SUM1)
end
for n=n+1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S5=F*(S1*S2*S3);
SUM2=SUM1+S5
figure
plot(x,SUM2)
end
for n=n+1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S6=F*(S1*S2*S3);
SUM3=SUM2+S6
figure
plot(x,SUM3)
end
for n=n+1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S7=F*(S1*S2*S3);
SUM4=SUM3+S7
figure
plot(x,SUM4)
end
for n=n+1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S8=F*(S1*S2*S3);
SUM5=SUM4+S8
figure
plot(x,SUM5)
end
for n=n+1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S9=F*(S1*S2*S3);
SUM6=SUM5+S9
figure
plot(x,SUM6)
end
for n=n+1
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S10=F*(S1*S2*S3);
SUM7=SUM6+S10
figure
plot(x,SUM7)
end
%What I thought might shorten code
clear
clc
h=input('What is height? ');
l=input('What is length? ');
w=input('What is frequency? ');
y=input('How many iterations? ');
x=0:0.001:l;
t=0;
F=(8*h)/(pi)^2;
n=1:y;
for n=1:y
i=2*n-1;
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S4=F*(S1*S2*S3);
SUM=sum(S4, i);
figure
plot(x,SUM)
end

Risposta accettata

mbonus
mbonus il 12 Set 2016
This is what it looks like you are trying to do to me
h=input('What is height? ');
l=input('What is length? ');
w=input('What is frequency? ');
y=input('How many iterations? ');
x=0:0.001:l;
t=0;%was this supposed to be a vector?
F=(8*h)/(pi)^2;
SUM = 0;
for n=1:y
i=2*n-1;
%%Update t here?
S1=((-1)^((i-1)/2))/(i^2);
S2=sin(i*pi*x/l);
S3=cos(w*t);
S4=F*(S1*S2*S3);
SUM = SUM + S4;
figure
plot(x,SUM)
title(['Plot of iteration ' num2str(i)])
end
Although you never modify t which will result in S3 always equaling 1, so maybe you meant to update t in the loop? Or did you mean to make t a vector like x?
  3 Commenti
mbonus
mbonus il 12 Set 2016
Glad to help. There have been a few times where I've had similar issues that you did with SUM, usually it goes down to using different capitalizations for the variable by mistake or maybe having an 's' at the end and then forgetting about it.
Image Analyst
Image Analyst il 13 Set 2016
I would not call the variable SUM. True, MATLAB is case sensitive so SUM is different than the built-in function named sum(), but still, it's better to not use the same name.
To be friendlier to the users, you might use a GUI to collect input numbers, like use inputdlg() instead of input(). Here's a code snippet/example that you can adapt:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 12 Set 2016
I have no idea what you really want to do. But this kind of syntax:
for n=n+1
while it may work, is nonsense. Please explain better what you're trying to do.
  1 Commento
polkerface
polkerface il 12 Set 2016
Maybe that shows how out of practice I am.
for n=n+1
was the "manual stepping" of the loop (plus the copy-paste of everything else between that and its subsequent end).
To attempt to clarify (still working from the code that that little piece was pulled from), I would like to run F*S1*S2*S3, get its total, plot it, then run it again with a larger n (which translates to a larger odd i), add its total to the previous total, plot this new total, and on and on until the number of iterations (y) has been reached.
I thought symsum, sum, etc. would be better options, but I cannot wrap my head around how to accomplish the running tally of totals and multiple plots (don't mean subplot, I mean a plot per each new total). Is that more clear, or did I muddy it further?

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by