Running a summation for a loop for different values of N.

2 ビュー (過去 30 日間)
Christopher
Christopher 2013 年 6 月 13 日
Hello,
I have written a function that basically uses a for loop to solve a summation from 1 to N values. I am trying to run this function for different values of N and plot the results on the same plot to compare convergence. So I would like to run the function for N=5, N=20, and N=100 and plot. What is the best way to run this function to solve for each of the different values of N and store them, so they can be plotted together? I was thinking something like:
a=1:N
b=2
c=4
for N=5
e=func(a,b,c,N)
end
for N=20
f=func(a,b,c,N)
end
for N=100
g=func(a,b,c,N)
end
plot(a,e,a,f,a,g)
Where a is length N and will be the x-axis of the plot. Is there a more efficient or better way to do this?

採用された回答

Jan
Jan 2013 年 6 月 13 日
N = [5, 20, 100];
result = zeros(1, length(N)); % Or what ever matchs your output
for iN = 1:length(N)
result(iN) = func(a, b, c, N(iN))
end
plot(a, result(1), a, result(2), a, result(3));
Perhaps you want something like:
result(iN, :) = func(a, b, c, N(iN))
% or
result(:, iN) = func(a, b, c, N(iN))
with a corresponding pre-allocation and modifications in the PLOT line.
  1 件のコメント
Christopher
Christopher 2013 年 6 月 14 日
編集済み: Christopher 2013 年 6 月 14 日
Jan,
When I input this it solves for the first value of N (5), but then when it tries to solve for the 2nd value of N (20) I get this error:
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Any ideas on what is causing this? The code is listed below:
function [LLT_solver] = LLT_run
global b N c a alpha alphaoL e AR
type=2 % read the input file using read_inp1.m
if type == 2
read_inp_type2;
N = [5, 20, 100];
result = zeros(1, length(N)); % Or what ever matchs your output
for iN = 1:length(N)
result(iN) = LLT_type2(b,N(iN),c,a,alpha,alphaoL,e,AR)
% LLT_type2(b,N,c,a,alpha,alphaoL,e,AR);
end
end
The function LLT_Type2 is:
function[An,CL,delta,CDi,Gamma,Cl]=LLT_type2(b,N,c,a,alpha,alphaoL,e,AR)
b;
N;
c=(c*ones(1,N))';
a=(a*ones(1,N))';
alpha=(alpha*ones(1,N))';
alphaoL=(alphaoL*ones(1,N))';
e=(e*ones(1,N))';
n=1:N; % n rows
theta=(n.*pi/(N+1))'; % theta values
y0=-b/2*cos(theta); % y values for each theta
RHS=(alpha+e-alphaoL)/180*pi; % Solving RHS
% Finding IC matrix to solve for An's
IC = zeros(N);
for l = 1:N;
for m = 1:N;
IC(l,m) = (4*b/a(l)/c(l)+m/sin(theta(l)))*sin(m*theta(l));
end
end
format long
An=IC\RHS
CL=An(1)*pi*AR
x=((n.*((An'./An(1))).^2));
delta=x(2:N);
format short
delta=sum(delta)
CDi=(CL^2/(pi*AR))*(1+delta)
% calculation of the vorticity Gamma on the wing
for i=1:N,
Gamma(i)=2*b*sum(An(:).*sin((1:N)*theta(i))');
end;
Gamma
Cl=(2*Gamma)./c'
plot(y0,Cl); ylabel('Cl / Vinf');
Thank you for your help. It's greatly appreciated.

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 6 月 13 日
編集済み: Andrei Bobrov 2013 年 6 月 13 日
N = [5 20 100]
b = 2;
c = 4;
f = arrayfun(@(x)[(1:x)',reshape(func((1:x)',b,c,x),[],1)],N,'un',0);
plot(f{:});
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2013 年 6 月 13 日
EDIT
Christopher
Christopher 2013 年 6 月 14 日
Andrei, how would I do this say if b was a scalar, but c was a column vector?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by