Trying to plot error

I'm trying to run this code and get plot of the error as a function of epsilon but im getting blank plot, someone can help?
%
clear all;close all;clc
epsilon=0.0001;
X = randn(2,1000); % make some input data
w = [1;1]; % define a linear function
n = randn(1,1000)*0.1; % produce some noise
Y = w'*X + n; % produce outputs
% scatter3(X(1,:),X(2,:),Y);
for iteration = 1 : 500 %in practice: until stopping
%criterion satisfied
grad = 2*sum(repmat(w'*X-Y,size(X,1),1).*X,2);
w = w - epsilon * grad;
err = sum((Y - w'*X).^2) %just to check
end
plot(err,epsilon)
end

Respuestas (1)

Star Strider
Star Strider el 17 de Abr. de 2018

0 votos

You never change ‘epsilon’ in your code, so it remains a single scalar value. You need to create a vector from it if you want to plot with respect to it.

If you want to create a vector from ‘err’, subscript it:

err(iteration) = sum((Y - w'*X).^2); %just to check

9 comentarios

rivaldo rivaldo
rivaldo rivaldo el 17 de Abr. de 2018
i'm still getting blank plot...there is any other way?
Star Strider
Star Strider el 17 de Abr. de 2018
You will get a blank plot unless you create a vector for °epsilon’ that is in some way related to it.
After you create a vector for ‘err’, I would just plot it as:
plot(err)
grid
rivaldo rivaldo
rivaldo rivaldo el 17 de Abr. de 2018
now it works thanks, but this solution is show me plot of the error as a function of the iterations. im looking for plot for the error as a function of epsilon.
Star Strider
Star Strider el 17 de Abr. de 2018
You never changed or updated ‘epsilon’, so it remains a single scalar value. Until you create it as a vector, you cannot plot with respect to it. It has to be a vector the same size as ‘err’ to work with the plot function.
You can plot it with respect to ‘w’ if you change your code in the loop to:
w = w - epsilon * grad;
wv(iteration) = w;
err(iteration) = sum((Y - w'*X).^2); %just to check
then after the loop:
plot(wv, err)
rivaldo rivaldo
rivaldo rivaldo el 17 de Abr. de 2018
i'm getting error
Subscripted assignment dimension mismatch.
Guillaume
Guillaume el 17 de Abr. de 2018
Editada: Guillaume el 17 de Abr. de 2018
Make sure to preallocate w and err before the loop. w is a vector so wv(iteration) = w is indeed an error.
numiterations = 500;
wv = zeros(numiterations , 2);
er =zeros(numiterations, 1);
for iteration = 1:numiterations
grad = 2*sum(repmat(w'*X-Y,size(X,1),1).*X,2);
w = w - epsilon * grad;
wv(iteration, :) = w;
err(iteration) = sum((Y - w'*X).^2) %just to check
end
Still no idea what you want to plot against. It can't be epsilon which never changes or w which is a 1x2 vector that changes at each iteration.
rivaldo rivaldo
rivaldo rivaldo el 18 de Abr. de 2018
i want to plot the error(err) against epsilon
Star Strider
Star Strider el 18 de Abr. de 2018
Please see my Answer and previous Comments.
You must create epsilon as a vector in order to plot with respect to it.
Try this:
epsilonv = ones(size(err))*epsilon; % Create Vector For ‘epsilon’
plot(epsilonv, err, 'p')
njj1
njj1 el 18 de Abr. de 2018
Editada: njj1 el 18 de Abr. de 2018
But epsilon is not changing.
What if you did this instead:
clear all;close all;clc
epsilon=0.0001:0.0001:0.0009; %nine entries into epsilon
X = randn(2,1000); % make some input data
w = [1;1]; % define a linear function
n = randn(1,1000)*0.1; % produce some noise
Y = w'*X + n; % produce outputs
numiterations = 500;
wv = zeros(numiterations , 2);
err =zeros(numiterations, 1);
for j = 1:numel(epsilon)
e = epsilon(j);
for iteration = 1:numiterations
grad = 2*sum(repmat(w'*X-Y,size(X,1),1).*X,2);
w = w - e * grad;
wv(iteration, :) = w;
err(iteration,j) = sum((Y - w'*X).^2); %just to check
end
end
plot(epsilon,err(end,:)); %plot err at the final iteration against epsilon

La pregunta está cerrada.

Preguntada:

el 17 de Abr. de 2018

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by