Function Return only one value
61 次查看(过去 30 天)
显示 更早的评论
My Code is as follows:
function [X,Y] = MyEKFFun(theta1,theta2,theta3,r1,r2,r3,t1,t2,t3,T)
S = [100 0 0 0 0 0;
0 100 0 0 0 0;
0 0 100 0 0 0;
0 0 0 100 0 0;
0 0 0 0 100 0;
0 0 0 0 0 100];
prev_S = S;
Big_lambda = eye(3);
a=0;
b=0;
c=0;
p=0;
q=0;
s=0;
est_vec=[ a ; b; c; p; q; s];
%theta = [ 31 24 18] ;
%t= [ 1 1.1 1.2 ];
%r= [330 364 379];
theta = [ theta1 theta2 theta3] ;
t= [ t1 t2 t3 ];
r= [r1 r2 r3];
%Wanted = num2cell(M,1)
%[r,b,distance,angle]=deal(Wanted{:})
x = [ r; theta ; t];
%dif_x=zeros(2,3);
dif_a = arrayfun(@(t) [t^2 t 1 0 0 0; 0 0 0 t^2 t 1],t,'UniformOutput',false);
%time=2;
time=T;
dif_x = zeros(2, 3, 3); % Pre-allocation
w = zeros(2, 1, 3); % Pre-allocation
%pred_x = zeros(3, 3, 3);
W = zeros(2,2);
y = zeros(2, 1, 3);
K = zeros(6, 2, 3);
for i=1:3
dif_x(:, :, i) = [-cos(theta(i)), r(i).*sin(theta(i)), 2*a.*t(i)+b;...
-sin(theta(i)), -r(i).*cos(theta(i)), 2*p.*t(i)+q];
W(:,:,i) = dif_x(:,:,i)*Big_lambda(i)*dif_x(:,:,i)';
pred_x = x(:,i)+randn(3,1);
w(:,:,i) = dif_x(:,:,i) * (x(:,i)-pred_x);
y(:,:,i) = dif_a{i} * est_vec + w(:,:,i);
K(:,:,i) = prev_S*dif_a{i}'/((W(:,:,i) + dif_a{i}*prev_S*dif_a{i}'));
est_vec_new = est_vec + K(:,:,i)*(y(:,:,i)-dif_a{i}*est_vec);
cond = abs(est_vec_new - est_vec);
if cond < 0.003
break
end
est_vec = est_vec_new;
a=est_vec(1,1);
b=est_vec(2,1);
c=est_vec(3,1);
p=est_vec(4,1);
q=est_vec(5,1);
s=est_vec(6,1);
S_new = (eye(6) - (K(:,:,i)*dif_a{i}))*prev_S;
prev_S = S_new;
%X(i) = a*time.^2+b*time+c; % To calculate x co-ordinate for t>=3
%Y(i) = p*time.^2+q*time+s; % To calculate y co-ordinate for t>=3
%figure,plot(t,meas_equa1)
%new_t=[t,time];
%figure,plot(r_cosTheta,r_sineTheta)
end
X = a*time.^2+b*time+c; % To calculate x co-ordinate for t>=3
Y = p*time.^2+q*time+s;
end
Now my question is why it returns only one values as 'ans'.It should be two values which is calculated as 'X,Y' from the updated values of the for loop.But the function always returns one value titled answer, like as follows:
>> MyEKFFun(31,24,18,330,364,379,1,1.1,1.3,2)
ans =
384.4037
Please help anyone.
Thanks in advance.
0 个评论
回答(2 个)
James Tursa
2019-5-15
编辑:James Tursa
2019-5-15
Call it with two requested outputs:
[X,Y] = MyEKFFun(31,24,18,330,364,379,1,1.1,1.3,2)
Since you were calling it with no requested outputs, MATLAB simply put the first one in ans and threw away the second one.
Jon Wieser
2019-5-15
when you call the function, you need to specify two outputs,
for example:
[X,Y] =MyEKFFun(31,24,18,330,364,379,1,1.1,1.3,2)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!