Need to make a nested loop

1 回表示 (過去 30 日間)
Tony Montgomery
Tony Montgomery 2014 年 10 月 4 日
コメント済み: Tony Montgomery 2014 年 10 月 5 日
I'm new at matlab, here's what i need to do. I've got a inner loop used to solve a polynomial at a given x value, But I need to evaluate the polynomial at 100 x-values. I need help making the loop work. here is what i have that does not work.
i = 1;
j = 0;
while j <= 100;
j = j+1;
%Loop used to find the solution to the polynomial at given point
while i <= n;
soln(j,i) = P(1,i)*(Xval(j)).^(n-i);
i = i + 1;
end
end
%soln
%Eval = sum(soln)
The inner loop (used to solution to the polynomial) does work. I need to run the inner loop 11 times(for a degree 10 polynomial) and the outter loop 100 times (for 100 xvalues). How can I make this work? I suck at matlab.
  2 件のコメント
per isakson
per isakson 2014 年 10 月 4 日
編集済み: per isakson 2014 年 10 月 4 日
Why not for-loops? Your question is it on loops or polynomials?
Tony Montgomery
Tony Montgomery 2014 年 10 月 4 日
My question is on loops. I need to solve a polynomial to 100 x values. I need those values stored into an array to be compared to the actual solution of the function I was given. My question is, How do i run the inner loop 10 times, for every 100 x values that i need?

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

回答 (3 件)

per isakson
per isakson 2014 年 10 月 4 日

per isakson
per isakson 2014 年 10 月 4 日
Is this what you try to do? With my variable names
n = 11;
P = 1 : n; % the simplest data I can think of
X = ones( 1, 100 ); % too allow me to check the result
S = nan( 100, n );
for ii = 1 : 100
for jj = 1 : n
S(ii,jj) = P(1,jj)*(X(ii)).^(n-jj);
end
end
>> S(1,:)
ans =
1 2 3 4 5 6 7 8 9 10 11

Image Analyst
Image Analyst 2014 年 10 月 4 日
Why not just polyfit() and not bother with all that mess?
coefficients = polyfit(P, Xval, 10); % 10 is way too high for a polynomial!
  1 件のコメント
Tony Montgomery
Tony Montgomery 2014 年 10 月 5 日
I did, but instructor will not accept. Must do it the long way. I found another way around just not very neat. thanks anyway

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

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by