Help replacing inner for loop with summation

8 次查看(过去 30 天)
Hello! I am having trouble with an academic problem; my professor would like me to 'replace the inner most for loop with two lines of code using .* as an operator and the sum function. I am utterly lost, because I think in for loops and this curveball feels outside the strike zone. Any insight would be awesome!
Question:Solutions to partial differential equations are often in the form of "Fourier series", which we will learn more about later in the course. Consider heat conduction in a one-dimensional rod of length L (0≤x≤L), with an initial temperature q(x, 0) = 0, the end at x = L kept insulated at all times (i.e. temperature gradient = 0) and the end at x = 0 increased to a temperature q0at time = 0 and maintained at q0thereafter.€∂θ∂t−α∂2θ∂x2=0, 0≤x≤L;θ(x,0)=0θ(0,t)=θ0 for t >0 and ∂θ∂x(L,t)=0 for all tHere, ais the thermal diffusivity of the rod, as an example, takehypothetical valuesof a= 1 and L = 1, and you can also considerq0= 1.The Fourier-series solution to the heat equation, which describes the evolution of the temperature distribution with time, is given by(note that the summation over n starts from n=0, not n=1):€θ(x,t)=θ01−4sin(2n+1)πx2Lexp−(2n+1)2π2αt4L2(2n+1)πn=0∞∑(a) We wish to plot the solution at several times (t = 0.01, 0.1, 0.2, 0.5, 1, 2, 5). For this we will compute the solution at each of these time values, at discrete points from x=0 to x=1, say using a spacing of 0.05, by summing the series at each of these values of x. Write a MATLAB script that will begin with an array of x values, and compute a matrix theta (7x21) corresponding to the 7 different time values for which we need the solution at 21 discrete x values. I want you to use 3 nested for loops, in which the outer-most loop changes the time value, the next loop goes over the discrete x values, and the innermost loop accomplishes the summationThe series sum cannot be carried to infinity, but truncated at some value of n. Note that this is not so bad, because as n increases, the exponential terminside the summation decreases rapidly. Let us simply use 50 terms in the series.I want you to plot (on a single graph) the temperature distributions versus x at the 7 different times and comment on the behavior you see in the plot.(b) Rewrite the script from Part(a) by replacing the inner-most loop that does the summation with two lines of MATLAB code that take advantage of MATLAB's vector-matrix capabilities (hint: think .* and search MATLAB help for "sum").
Code:
theta0=1;
L=1;
alpha=1;
n=0;
a=zeros(7,21);
b=[];
c=1;
d=1;
for t=[0.01, 0.1, 0.2, 0.5, 1, 2, 5];
for x=0:0.05:length(L);
for n=0:50;
y=(((sin(2*n+1)*pi()*x)/(2*L))*exp((-(2*n+1)^2*pi()^2*...
alpha*t)/(4*L^2)))/((2*n+1)*pi());
theta=theta0*(1-4*(y));
b(n+1)=theta;
z=sum(b);
end
a(c,d)=z;
d=d+1;
end
d=1;
c=c+1;
end
x=0:0.05:1;
t=[0.01, 0.1, 0.2, 0.5, 1, 2, 5];
figure
plot(x,a)
title('Rod temperature distribution')
xlabel('Length of Rod, x')
ylabel('Temperature, T')

回答(1 个)

Mudambi Srivatsa
Mudambi Srivatsa 2016-9-26
I understand that you are trying to eliminate the innermost loop using ".*" and "sum" functions. MATLAB provides operators to perform element-wise operations on arrays instead of using loops.
In the following code, 'y' is computed using one value of 'n' at a time.
for n=0:50
y=(((sin(2*n+1)*pi()*x)/(2*L))*exp((-(2*n+1)^2*pi()^2*...
alpha*t)/(4*L^2)))/((2*n+1)*pi());
theta=theta0*(1-4*(y));
b(n+1)=theta;
z=sum(b);
end
The same computation can be performed in an efficient way in MATLAB as follows:
n = 0:50; % define at the start of the program
y = (((sin(2*n+1)*pi()*x)/(2*L)).*exp((-(2*n+1).^2*pi()^2*...
alpha*t)/(4*L^2)))./((2*n+1)*pi());
theta = theta0*(1-4*(y));
z = sum(theta);
Notice that 'y' and 'theta' are vectors with values computed for all values of array 'n'. The 'theta' values can be added up without the temporary array 'b' using "sum" function. You should notice speedup in the execution after replacing the loop with element-wise operations.
For more information, refer to the following documentation pages:

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by