can't do this

조회 수: 1 (최근 30일)
Tom
Tom 2012년 1월 8일
can't do this
Hi, I'm attempting nested for loops and failing miserably.
Here's my code: -
close all;
clear all;
l=50;R=100;T=68;m=0.000125;w0=0.1;t=0;
mu=m/l;c=T/mu;
r=2;x0=l/r;
iter=1;
for x=1:33;
iter=1;
for n=1:5000;
wxt=((4*w0)/pi)*exp(-R*t)*((l/(pi*n^2*x0))*sin((n*pi*x0)/l)-(1/n)*cos((n*pi*x0)/l)+(l/(2*n*x0))*cos((n*pi*x0)/l))*sin((n*pi*x)/l)*cos((c*n*pi*t)/l);
wxtrec(iter)=wxt;
iter=iter+1;
end
w=sum(wxtrec)
wrec(iter)=w;
iter=iter+1;
end
plot(x,wrec)
For the first outer loop run (x=1), I want the inner loop to do 5000 circuits, then I want the sum of that result. Then I want it to go around again 5000 times for x=2, taking a sum of that result etc.
Once I have 33 summed results, I want to plot those sums against x
  댓글 수: 1
Image Analyst
Image Analyst 2012년 11월 3일
Next time, could you pick a more meaningful subject line than "can't do this"?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 8일
With minimal changes. Improvements are possible.
close all;
clear all;
l=50;R=100;T=68;m=0.000125;w0=0.1;t=0;
mu=m/l;c=T/mu;
r=2;x0=l/r;
iter=1;
for x=1:33;
iter=1;
for n=1:5000;
wxt=((4*w0)/pi)*exp(-R*t)*((l/(pi*n^2*x0))*sin((n*pi*x0)/l)-(1/n)*cos((n*pi*x0)/l)+(l/(2*n*x0))*cos((n*pi*x0)/l))*sin((n*pi*x)/l)*cos((c*n*pi*t)/l);
%wxtrec(iter)=wxt;
wxtrec(n)=wxt; %NEW
iter=iter+1;
end
w=sum(wxtrec)
%wrec(iter)=w;
wrec(x)=w; %NEW AND IMPROVED
iter=iter+1;
end
plot(x,wrec)
  댓글 수: 2
Tom
Tom 2012년 1월 9일
Hi thanks for answering. The code returns a message about subscript indices having to be either real positive integers or logicals. Should I be changing something?
Walter Roberson
Walter Roberson 2012년 1월 9일
Sorry, I had a typo which I have now corrected.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by