How to combine a while loop and a for loop on one graph

조회 수: 3 (최근 30일)
Nicholas
Nicholas 2014년 9월 19일
댓글: Image Analyst 2014년 9월 19일
the while loop is set up as such
fig=figure(1);
x(1)=0;
fx(1) = 0;
index = 2;
while (x<20) & index < 1000
x(index)=x(index-1)+1
fx(index)= x(index)^3 - (5*x(index))^2 + 2^(x(index)) - 10000.*x(index);
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 the for loop set up as such
fig=figure(1);
x(1)=0;
fx(1) = -3;
index = 2;
for index = 2 : 100000
x(index)=x(index-1)+0.5
fx(index)= 20000*log(x(index))-3*x(index);
clf;
grid on;
hold on;
xlabel('x', 'FontSize', 10);
ylabel('fx', 'FontSize', 10);
title('For Loop', 'FontSize', 10);
p = plot(x, fx, 'bo-', 'LineWidth',4, 'MarkerSize', 10);
% Exit loop if x >= 20
if x(index) >= 20
break;
end
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
If anyone would be willing to help me find how to place both on one graph I would greatly appreciate it!
The while loop function is f(x) = x^3 - (5*x)^2 + 2^(x) - 10000.*x 0<x<20
The for loop function is f(x) = 20000*log(x) - 3*x 1<x<20

채택된 답변

Image Analyst
Image Analyst 2014년 9월 19일
Try 3*x(index) instead of 3*x.
  댓글 수: 2
Nicholas
Nicholas 2014년 9월 19일
Thank you! Do you know how I can get the two onto one graph?
Image Analyst
Image Analyst 2014년 9월 19일
Like I said in your duplicate question:
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by