Lots of errors - too many to list. Just study corrected code:
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
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);
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
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.
Best Answer