erorr in for loop

4 ビュー (過去 30 日間)
Lilya
Lilya 2016 年 5 月 2 日
コメント済み: Lilya 2016 年 5 月 3 日
could you please help me to correct these small calculations
w=0:0.02:5;
for i=1:length(w)
a(i)=w.^4-12.*(w.^2);
b(i)=w.^8-144.*(w.^4);
c(i)=9.*(w.^6)+256.*(w.^2);
Real(i)= a(i)/(b(i)+c(i));
end
thank you

採用された回答

CS Researcher
CS Researcher 2016 年 5 月 2 日
.^ is for element wise operation. Use this:
w=0:0.02:5;
for i=1:length(w)
a(i)=w(i)^4-12*(w(i)^2);
b(i)=w(i)^8-144*(w(i)^4);
c(i)=9*(w(i)^6)+256*(w(i)^2);
Real(i)= a(i)/(b(i)+c(i));
end
Or better:
w=0:0.02:5;
a = w.^4-12.*(w.^2);
b = w.^8-144.*(w.^4);
c = 9.*(w.^6)+256.*(w.^2);
Real= a./(b+c);
  3 件のコメント
CS Researcher
CS Researcher 2016 年 5 月 2 日
What is x?
Lilya
Lilya 2016 年 5 月 2 日
編集済み: Lilya 2016 年 5 月 2 日
Sorry for the mistake Finding the w value that makes the equation =1 (i.e Real)

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 5 月 2 日
Try this:
w=0:0.02:5;
a = w.^4-12.*(w.^2);
b = w.^8-144.*(w.^4);
c = 9.*(w.^6)+256.*(w.^2);
ratios = a ./ (b+c);
plot(w, ratios, 'b*-');
grid on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Find w where ratios == 1
differences = ratios-1;
[minDifference, indexOfMin] = min(differences)
% Plot a circle there
hold on;
plot(w(indexOfMin), ratios(indexOfMin), 'ro', 'MarkerSize', 15, 'LineWidth', 2);
message = sprintf('ratios is closest to 1 at index %d where ratios(%d) = %f',...
indexOfMin, indexOfMin, w(indexOfMin));
uiwait(helpdlg(message));
  1 件のコメント
Lilya
Lilya 2016 年 5 月 3 日
thanks alot

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by