필터 지우기
필터 지우기

Undefined function or variable 't'.

조회 수: 2 (최근 30일)
jason law
jason law 2020년 5월 13일
편집: Stephen23 2020년 5월 13일
I am trying on solving optimization problem but i gt the error
'Undefined function or variable 't'.
clear
clc
close all
%%
load('C:\Users\User\Desktop\fyp\defect_free(H).mat');
load('C:\Users\User\Desktop\fyp\f_column.mat');
load('C:\Users\User\Desktop\fyp\fi_column.mat');
load('C:\Users\User\Desktop\fyp\fr_column.mat');
fi=fi_column;
fr=fr_column;
f=f_column';
abs_H=abs(ifft(H));% getting magnitude of H from data in frequency domain
t1=[sort(rand(101,1))];
t2=[sort(rand(101,1))];
t=[t1 t2];
%%
problem = createOptimProblem('fmincon',...
'objective',@(t)obj(t1, t2,f,fr,fi),...
'x0',[sort(rand(101,1)),sort(rand(101,1))],'options',...
optimoptions(@fmincon,'Algorithm','sqp','Display','off'));
[x,fval] = fmincon(problem)
gs = GlobalSearch('Display','iter');
[x,fval] = run(gs,problem)
%%
function J=obj(t1, t2,f,fr,fi)
for x=1:101 % number of frequency
for y=1:101 %number of t
Fr(x,y)=cos(2*pi*f(x)*t(y)); %real number of F
Fi(x,y)=-i*sin(2*pi*f(x)*t(y)); % imaginary number of F
end
end
Fr_T=Fr';
Fi_T=Fi';
A=(Fr_T*Fr)+(Fi_T*Fi);
B=(Fr_T*fr)+(Fi_T*fi);
a=lsqminnorm(A,B) ; % solve tp get a set of a
J=sum((a-abs_H))^2 ;% to find the minimum of J from measured data
end
'
  댓글 수: 1
Stephen23
Stephen23 2020년 5월 13일
편집: Stephen23 2020년 5월 13일
There are a few issues related to t:
1- Your anonymous function has one input argument t, which is never used:
@(t)obj(t1, t2,f,fr,fi)
2- Inside the function obj you refer to a variable/function/class t which is undefined in the function.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2020년 5월 13일
There are several problems with your code. The one causing the error is that you are using t in your inner loop in your obj function. It also looks like that nested loop could be an array operation, but without your data it is hard to confirm.
Speaking of data: you are poofing variables into existance with those load operations. You should load to a struct instead. That way you can show where each variable is coming from.
As a last point: don't use clear all. The usefulnes of close all is also questionable here, as you don't open any figures. If you want to clear your variables, use clear or clearvars. Outside of debugging you should be using functions to keep your workspace clean.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by