我有一个脚本文件,可以从Excel电子表格加载数据并调用函数:
clear;clc
offsets = [0 0];
crossed = 0; % fourbar is open, not crossed
L = xlsread('lab5data.xlsx','A:A')/100;
theta_s = xlsread('lab5data','B:B'); % Servo angles [deg]
theta_l_exp = xlsread('lab5data.xlsx','C:C');
sum_sq_error = fourbar_calibration( offsets,L,crossed,theta_s,theta_l_exp)
fc_handle = @(offsets,L,crossed,theta_s,theta_l_exp)
fourbar_calibration(offsets,L,crossed,theta_s,theta_l_exp);
optimal_offsets = fminsearch(fc_handle,[15 15],[],sum_sq_error);
脚本调用此函数:
function [ sum_sq_error ] = fourbar_calibration(
offsets,L,crossed,theta_s,theta_l_exp)
[theta_l] = fourbar( L, crossed, theta_s, offsets );
sum_sq_error = sum((theta_l_exp-theta_l).^2)
end
该函数在运行时调用第二个函数来计算theta_l:
function [ theta_l ] = fourbar( L, crossed, theta_s, offsets )
[theta_2] = 180 - theta_s + offsets(1);
K1 = L(1)/L(2); % fourbar K values
K2 = L(1)/L(4);
K3 = ( L(1)^2 + L(2)^2 - L(3)^2 + L(4)^2 ) / ( 2 * L(2) * L(4) );
A = cosd(theta_2) - K1 - K2.*cosd(theta_2) + K3; % Quadratic input A
B = -2.*sind(theta_2); % Quadratic input B
C = K1 - (K2 + 1).*cosd(theta_2) + K3; % Quadratic input C
if crossed == 1 % Angle of rocker w.r.t. ground link if fourbar is crossed
theta_4 = 2.*atand( (-B + sqrt(B.^2 - 4.*A.*C)) ./ (2.*A));
real_num_check = B.^2 - 4.*A.*C; % Creates array of values to check for invalud servo values
check_index = find(real_num_check<1);
theta_4(check_index) = NaN;
if length(check_index) == length(theta_4)
error('No valid servo angles - there is a mistake somewhere!!!')
end
if nargout == 0
theta_l = 180 - theta_4 + offsets(2); % Launch angle w.r.t. horizontal
plot(theta_s,theta_l)
title('Launch Angles vs Servo Angles')
xlabel('Servo Angles [deg]')
ylabel('Launch Angles [deg]')
grid on
end
[theta_l] = 180 - theta_4 + offsets(2); % Launch angle w.r.t. horizontal
end
if crossed == 0 % Angle of rocker w.r.t. ground link if fourbar is open
[theta_4] = 2.*atand( (-B - sqrt(B.^2 - 4.*A.*C)) ./ (2.*A));
real_num_check = B.^2 - 4.*A.*C; % Creates array of values to check for invalud servo values
check_index = find(real_num_check<1);
theta_4(check_index) = NaN;
if length(check_index) == length(theta_4)
error('No valid servo angles - there is a mistake somewhere!!!')
end
if nargout == 0
[theta_l] = 180 - theta_4 + offsets(2); % Launch angle w.r.t. horizontal
plot(theta_s,theta_l)
title('Launch Angles vs Servo Angles')
xlabel('Servo Angles [deg]')
ylabel('Launch Angles [deg]')
grid on
end
[theta_l] = 180 - theta_4 + offsets(2); % Launch angle w.r.t. horizontal
end
当我运行脚本文件时,收到以下错误:
**Error using @(offsets,L,crossed,theta_s,theta_l_exp)fourbar_calibration(offsets,L,crossed,theta_s,theta_l_exp)
Not enough input arguments.
Error in fminsearch (line 190)
fv(:,1) = funfcn(x,varargin{:});
Error in HW5PPP2 (line 28)
optimal_offsets = fminsearch(fc_handle,[15 15],[],sum_sq_error);**
我已经尝试通过添加theta_l并删除所有额外输入来更改fminsearch参数末尾的输入,以及更改函数的输入,但是我得到错误告诉我输入太多或者没有足够。我已经检查了其他论坛帖子,但仍然不明白为什么我在这种特殊情况下收到错误。
我还想保存在第二个函数中间计算的值 theta_l ,但我不知道如何在运行完毕后将其保存到我的工作区。< / p>