I am brand new to MATLab and do not know how to do a whileloop

1 回表示 (過去 30 日間)
Nicholas
Nicholas 2014 年 9 月 19 日
コメント済み: Image Analyst 2014 年 9 月 19 日
x=0;
while (x<20)
x=x+1
f(x)= x^3 - (5*x)^2 + 2^(x) - 10000.*x;
fig=figure(1); clf; grid on; hold on;
xlabel('x1'); ylabel('y1'); title('While Loop');
p = plot(Value(:,1),Store(:,1));
set(p,'Color','red','LineWidth',2);
end
what is wrong and why will it not plot? everything from fig=figure(1); on is given

採用された回答

Image Analyst
Image Analyst 2014 年 9 月 19 日
Lots of errors - too many to list. Just study corrected code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fig=figure(1);
x(1)=0;
fx(1) = 0;
index = 2;
while (x<20) & index < 1000 % Add index check as a failsafe.
x(index)=x(index-1)+0.5
fx(index)= x(index)^3 - (5*x(index))^2 + 2^(x(index)) - 10000.*x(index);
clf;
grid on;
hold on;
xlabel('x', 'FontSize', fontSize);
ylabel('fx', 'FontSize', fontSize);
title('While Loop', 'FontSize', fontSize);
p = plot(x, fx, 'ro-', 'LineWidth',2, 'MarkerSize', 10);
index = index + 1;
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
Of course I hope you know you could do this without a while loop in just a few lines, but I guess you were interested in the while loop mechanics rather than just how to plot a curve.
  5 件のコメント
Nicholas
Nicholas 2014 年 9 月 19 日
how would i put a while loop and a for loop on the same graph if the for loop's function is f(x) = 20000*log(x)-3x I tried to almost repeat it like this
fig=figure(1);
x(1)=1;
fx(1)=-3;
for (x<20) & index < 1000
x(index) = x(index)+1
fx(index) = 20000*log(x(index)) - 3*x
clf;
grid on;
hold on;
xlabel('x', 'FontSize', 10);
ylabel('fx', 'FontSize', 10);
title('While Loop', 'FontSize',10);
p = plot(x, fx, 'ro-', 'LineWidth',2, 'MarkerSize', 10);
index = index + 1;
end
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1])
and to just place in the while loop operation to give me something like this
fig=figure(1);
x(1)=0;
fx(1) = 0;
index = 2;
while (x<20) & index < 1000
x(index)=x(index-1)+0.5
fx(index)= x(index)^3 - (5*x(index))^2 + 2^(x(index)) - 10000.*x(index);
fig=figure(1);
x(1)=1;
fx(1)=-3;
for (x<20) & index < 1000
x(index) = x(index)+1
fx(index) = 20000*log(x(index)) - 3*x
clf;
grid on;
hold on;
xlabel('x', 'FontSize', 10);
ylabel('fx', 'FontSize', 10);
title('While Loop', 'FontSize',10);
p = plot(x, fx, 'bo-', 'LineWidth',2, 'MarkerSize', 10);
index = index + 1;
clf;
grid on;
hold on;
xlabel('x', 'FontSize', 10);
ylabel('fx', 'FontSize', 10);
title('While Loop', 'FontSize',10);
p = plot(x, fx, 'ro-', 'LineWidth',2, 'MarkerSize', 10);
index = index + 1;
end
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1])
but that was wrong.. How can I place them both on one graph?
Image Analyst
Image Analyst 2014 年 9 月 19 日
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
x(1)=0;
fx(1) = 0;
index = 2;
while (x<20) & index < 1000 % Add index check as a failsafe.
x(index)=x(index-1)+0.5;
fx(index)= x(index)^3 - (5*x(index))^2 + 2^(x(index)) - 10000.*x(index);
p = plot(x, fx, 'ro-', 'LineWidth',2, 'MarkerSize', 10);
index = index + 1;
end
grid on;
hold on;
xlabel('x', 'FontSize', fontSize);
ylabel('fx', 'FontSize', fontSize);
title('While Loop', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
x2(1)=0;
fx2(1) = 0;
index = 2;
for index = 2 : 100000
x2(index)=x2(index-1)+0.5;
fx2(index) = 20000*log(x2(index)) - 3*x2(index);
p = plot(x2, fx2, 'bo-', 'LineWidth',2, 'MarkerSize', 5);
% Exit loop if x >= 20
if x(index) >= 20
break;
end
end
grid on;
xlabel('x', 'FontSize', fontSize);
ylabel('fx', 'FontSize', fontSize);
title('Both Loop', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

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

その他の回答 (0 件)

カテゴリ

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