Too many output arguments (five on output)
1 次查看(过去 30 天)
显示 更早的评论
Code below. Getting problems with "too many output", that's strange, cause cub_def has the same (5) output variables. Tried to do it with Fuction [a_spline, a_coeff_spline]=cub_def(x, a, x, 0, 0); and then call just
[a_spline, a_coeff_spline]=cub_def(x, a, x, 0, 0) - didn't help
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, 'Color', '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
%Runge-Kutta method for regression dependence
V_num_RK=zeros(size(x));
%initial values
V_num_RK(1)=V0;
for i=2:length(x)
K1=a(i-1);
K2=(x(i-1)+dx/2).*sqrt(b1*sin(2*pi*b3*x(i-1)+dx/2)/(b2^2+(x(i-1)+dx/2).^2));
K3=K2;
K4=a(i);
V_num_RK(i)=V_num_RK(i-1)+dx/6*(K1+2*K2+2*K3+K4);
end
%Runge-Kutta method for interpolation spline
addpath(pwd);
[a_spline, a_coeff_spline]=cub_def(x, a, x, 0, 0);
for i=2:length(x)
K1=a(i-1);
K2=(x(i-1)+dx/2).*sqrt(a_coeff_spline(i-1, 1)*(dx/2)^3+a_coeff_spline(i-1, 2)*(dx/2)^2+...
a_coeff_spline(i-1, 3)*(dx/2)+a_coeff_spline(i-1, 4));
K3=K2;
K4=a(i);
V_num_RK_sp(i)=V_num_RK(i-1)+dx/6*(K1+2*K2+2*K3+K4);
end
figure
plot(x, V_num_Eu, 'Color', 'y', x, V_num_RK, 'Color', 'g', x, V_num_RK_sp, 'Color', 'r');
hold on
function y_cubic=cub_def(x, y, x_cubic, lambda_1, lambda_n);
%number of experimental points;
n=length(x);
%vectors variables for spline coefficients
a=zeros(n-1,1);
b=a;
c=a;
d=y(1:end-1);
h=diff(x);
delta=diff(y);%finite differences+transposition
phi=delta./h;
theta=h(1:end-1)./h(2:end);
%first and dummy (selected) b_n
b(1)=lambda_1/2;
b(end+1)=lambda_n/2;
%vector of free terms for the matrix search system of all b
%all the same except the first and last
right_side =3./h(2:end).*diff(phi);
right_side(1)=right_side(1)-theta(1)*lambda_1/2;
right_side(end)=right_side(end)-lambda_n/2;
%tridiagonal matrix
%for convenience, there are 3 columns in 1 variable
m=zeros(n-2,3);
%main diagonal of the matrix
m(:,2)=2*(1+theta);
%side diagonals
m(1:end-2,1)=theta(2:end-1);
m(1:end,3)=ones(n-2,1);
M=spdiags(m, [-1 0 1], n-2, n-2);
%calculation of all 's except those already set
b(2:end-1)=M\right_side;
%calculation of all c's except those already set
c=phi-h/3.*(2*b(1:end-1)+b(2:end));
%calculation of all a's
a=1./(h.^2).*(phi-c)-b(1:end-1)./h;
%all values for the spline
y_cubic=zeros(size(x_cubic));
%pass through all intervals [x(i), x(i+1)] between experimental points
%number of points to build a spline
l=length(x_cubic);
y_cubic=zeros(size(x_cubic));
for i=1:n-1
%pass through all x_cube to see if x_cube(j) is in the interval [x(i), x(i+1)]
for j=1:l
if x_cubic(j)>=x(i) && x_cubic(j)<x(i+1)
y_cubic(j)=a(i)*(x_cubic(j)-x(i))^3+b(i)*(x_cubic(j)-x(i))^2+...
c(i)*(x_cubic(j)-x(i))+d(i);
end
end
end
%for the last point separately
if x_cubic(end)==x(end);
y_cubic(end)==y(end);
end
end
0 个评论
回答(1 个)
Les Beckham
2022-7-11
You are defining this function as a function that takes 5 input arguments and returns one output argument.
function y_cubic=cub_def(x, y, x_cubic, lambda_1, lambda_n);
However, when you call it you are asking for two output arguments.
[y_cub, coeff]=cub_def(x, y, x_cubic, lambda_1, lambda_n);
If you need the function to return two arguments, you must define it that way.
2 个评论
Les Beckham
2022-7-12
What do you mean by "it still doesn't work"? Do you get an error message? If so, please post the complete error message (everything in red).
Also, I don't see the variable 'coeff' anywhere in the cub_def function so using that variable as a return argument won't work.
Another thing I just noticed is that
addpath(pwd)
is completely unnecessary. Matlab always searches in the current directory first.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!