Error using plot Data must be a single matrix Y or a list of pairs X,Y.
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dmitry Surov
el 11 de Jul. de 2022
Comentada: Voss
el 11 de Jul. de 2022
Code below, misunderstand how to correct eror "Error using plot Data must be a single matrix Y or a list of pairs X,Y.".
clc;
close all;
imtool close all;
clear;
workspace;
% Read data
[filename, filepath]=uigetfile;
data_from_file=dlmread([filepath, filename],';', 3, 0);
% Read data
x=data_from_file(:,1);
y=data_from_file(:,2);
%Max Iterations
i_max=10000;
%Reg coef limits
A_min=-3;
A_max=3;
alpha_min=0.25;
alpha_max=1;
f_min=0.158;
f_max=1;
%Temperature
t_min=0;
t_max=1;
%Temperature decrease law
denom=1:1:i_max-1;
t=t_max./denom;
t(length(t)+1)=t_min;
% initial approximation
b_pribl_anneal=[295, 140, 7.559];
%array for calculating the residual function at the i-th iteration
sigma=zeros(i_max, 1);
%array with residual function for response
sigma_goal=0.5*sum((b_pribl_anneal(1).*sin(2*pi*b_pribl_anneal(3).*x)./(b_pribl_anneal(2)^2+x.^2))-y.^2).*ones(size(sigma));
%calculate the first value of the residual function by substituting
%the coefficients from the initial approximation
sigma_goal=0.5*sum((b_pribl_anneal(1).*sin(2*pi*b_pribl_anneal(3).*x)./(b_pribl_anneal(2)^2+x.^2))-y.^2);
%create fields for charts and give them names
plot_anneal_figure=figure();
for i=1:i_max;
if mod(i, 10000)==0 || i==1;
%build a graph with experimental points and the calculated model
figure(plot_anneal_figure);
plot(x, y, 'LineStyle', 'none', 'Marker', '*');
hold on;
%solution at the current iteration
plot(x, b_pribl_anneal(1).*sin(2*pi*b_pribl_anneal(3).*x)./(b_pribl_anneal(2)^2+x.^2), 'LineWidth', 2, 'Color', 'r') ;
legend('Experimental data', ['Simulated annealing', num2str(i) 'iterations']);
hold off;
end
%calculate sigma at the current iteration
sigma(i)=0.5*sum((b_pribl_anneal(1).*sin(2*pi*b_pribl_anneal(3).*x)./(b_pribl_anneal(2).^2+x.^2))-y.^2);
%generate candidate for solution
b_shift=[rand()*(A_max-A_min) rand()*(alpha_max-alpha_min) rand()*(f_max-f_min)]*(-1)^randi([0 1])*t(i);
b_pribl_anneal_new=b_pribl_anneal+b_shift;
%check if we stay within the allowed range
if b_pribl_anneal_new(1)>A_max || b_pribl_anneal_new(1)<A_min;
b_pribl_anneal_new(1)=b_pribl_anneal(1);
end
if b_pribl_anneal_new(2)>alpha_max || b_pribl_anneal_new(2)<alpha_min;
b_pribl_anneal_new(2)=b_pribl_anneal(2);
end
if b_pribl_anneal_new(3)>f_max || b_pribl_anneal_new(3)<f_min;
b_pribl_anneal_new(3)=b_pribl_anneal(3);
end
%use the residual function to calculate the probability of a jump
%we calculate the residual function for the candidate for the solution
sigma_new=0.5*sum(((b_pribl_anneal_new(1).*sin(2*pi*b_pribl_anneal_new(3).*x))./(b_pribl_anneal_new(2)^2+x.^2)-y).^2);
%choose a solution for the next iteration
if sigma_new<sigma(i)
b_pribl_anneal=b_pribl_anneal_new;
else
%the difference in the "energies" of the candidates for the solution
d_sigma=sigma_new-sigma(i);
%jump probability
P=exp(-d_sigma/t(i));
end
if P>=rand();
b_pribl_anneal=b_pribl_anneal_new;
end
end
%Interpolation of experimental points using the cubic spline of defect 1
%set of points to interpolate
x_cubic=x;
lambda_1=0;
lambda_n=0;
%calculation of cubic spline of defect 1
[y_cub, coeff]=cub_def(x, y, x_cubic, lambda_1, lambda_n);
%plot of calculated data
figure;
plot(x_cubic, y_cub, 'LineWidth', 3, 'g');
hold on;
plot(x, y, 'LineStyle', 'none', 'Marker', '*');
grid on;
%First order differential equation solution
b1=b_pribl_anneal_new(1);
b2=b_pribl_anneal_new(2);
b3=b_pribl_anneal_new(3);
V0=0;
dx=x(2)-x(1);
%improved Euler method for a discrete set of points
V_num_Eu=zeros(size(x));
%initial values
V_num_Eu(1)=V0;
%vector with discrete values
a=x.*sqrt(b1*sin(2*pi*b3*x)/(b2^2+x.^2));
for i=2:length(x)
V_num_Eu(i)=a(i)*dx+V_num_Eu(i-1);
end
0 comentarios
Respuesta aceptada
Voss
el 11 de Jul. de 2022
Editada: Voss
el 11 de Jul. de 2022
Try changing this line:
plot(x_cubic, y_cub, 'LineWidth', 3, 'g');
to this:
plot(x_cubic, y_cub, 'LineWidth', 3, 'Color', 'g');
% Name, Value, Name, Value
or this:
plot(x_cubic, y_cub, 'g', 'LineWidth', 3);
% LineSpec, Name, Value
Name-Value argument pairs must come at the end.
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!