MATLAB: Hi guys,i am beginner at matlab.Can you help me with that?I have a problem which is not plotting the values.

figureMATLABplottingtable

clc
clear all
close all
Ea = 100;
m = 0;
i = 0;
x0 = 0.7;
while Ea > 10^-3
if m < 50
x1 = x0 - ((x0^3 - x0 - 3) ./ ((3 * x0)^2 - 1));
Ea = abs(((x1 - x0) / x1) * 100);
fprintf('%2.0f %12.5f %12.5f %12.5f %12.5f\n', i + 1, x0, (x0^3 - x0 - 3), ((3 * x0)^2 - 1), Ea);
plot(Ea,i,'b')
xlabel('|Ea|');
ylabel('i');
title('|Ea| versus i');
plot(x0,i,'b')
xlabel('x0');
ylabel('i');
title('x0 versus i');
i = i + 1;
x0 = x1;
else
break
end
m = m + 1;
end

Best Answer

  • It is best to take the plot call out of the loop, and save thevariables as vectors to plot later.
    Try this:
    Ea = 100;
    m = 0;
    i = 0;
    x0 = 0.7;
    while Ea > 10^-3
    if m < 50
    x1 = x0 - ((x0^3 - x0 - 3) ./ ((3 * x0)^2 - 1));
    Ea = abs(((x1 - x0) / x1) * 100);
    fprintf('%2.0f %12.5f %12.5f %12.5f %12.5f\n', i + 1, x0, (x0^3 - x0 - 3), ((3 * x0)^2 - 1), Ea);
    i = i + 1;
    x0 = x1;
    x0v(i) = x1;
    Eav(i) = Ea;
    iv(i) = i;
    else
    break
    end
    m = m + 1;
    end
    figure
    plot(Eav,iv,'b')
    xlabel('|Ea|');
    ylabel('i');
    title('|Ea| versus i');
    figure
    plot(x0v,iv,'b')
    xlabel('x0');
    ylabel('i');
    title('x0 versus i');
    You may want to reverse the order of the arguments to the plot calls, since the independent variable is the first argument, and the dependent variable is the second argument as the plot function is defined in MATLAB.
    Experiment to get the result you want.