MATLAB: Error in Using 'fminsearch' function - matlab

I am doing a small research that requires to find the argmin of some function. This is the function I wrote. I want to find a vector x that gives the minimum output of this function. I tried fminsearch(#crc,x), but I got the following
error.
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Any help or hint would be appreciated.
function crc = SQ(x)
pdfx = gampdf([0:89],x(1),1/x(2));
pdfy = gampdf([0:89],x(3),1/x(4));
pdfz = conv(pdfx,pdfy);
cdfz= cumsum(pdfz);
h=pdfz./(1-cdfz);
% I = piecewise(0<t<=9, 0.1, 9<t<=14, 0.3, 14<t<=19, 0.9, ...
% 19<t<=24, 1.5, 19<t<=29, 3, 29<t<=34, 5.7, 34<t<=39, 10.5, 39<t<=44, 19,...
% 44<t<=49, 33.1, 49<t<=54, 59.5, 54<t<=59, 68.4, 59<t<=64, 90.2, 64<t<=69,...
% 121.4, 69<t<=74, 153, 74<t<=79, 197.5, 79<t<=84, 237.9, 84<t<=89, 258.8);
I=zeros(1,161);
for t=0:1:89
if 0 < t && t <= 9
I(t)=.1;
elseif 9<t&& t <=14
I(t) = 0.3;
elseif 14<t && t<=19
I(t) = 0.9;
elseif 19<t&& t <=24
I(t) = 1.5;
elseif 24<t&& t <=29
I(t) = 3;
elseif 29<t&& t <=34
I(t) = 5.7;
elseif 34<t&& t <=39
I(t) = 10.5;
elseif 39<t&& t <=44
I(t) = 19;
elseif 44<t&& t <=49
I(t) = 33.1;
elseif 49<t&& t <=54
I(t) = 59.5;
elseif 54<t&& t <=59
I(t) = 68.4;
elseif 59<t&& t <=64
I(t) = 90.2;
elseif 64<t&& t <=69
I(t) = 121.4;
elseif 69<t&& t <=74
I(t) = 153;
elseif 74<t&& t <=79
I(t) = 197.5;
elseif 79<t&& t <=84
I(t) = 237.9;
elseif 84<t&& t <=89
I(t) = 258.8;
end
end
I=I/100000;
sqer = sum((h([20:89])-I([20:89])).^2);
end

The error massage you posted looks incompleted. It will be clearer if you show us the full error message.
Your function is called SQ; crc is the name of its output variable.
Note also that matlab identifies functions by their file names. I assume your function file is named SQ.m.
Try
fminsearch(#SQ,x)

Related

How to define a peicewise function in a function file - Matlab

I made a funnction file and defined a peicewise function inside it using conditionals and a for loop. I tried calling the function in a seperate m.file but the variables 't' and 'v' aren't showing in the workspace, rather it is just outputting a vector with the t values called 'ans.
I tried putting the exact code (without the function definition) into a regular m file and it worked just fine showing both variables t and v
#function file
function [t, v] = VPieceWise(t_start, t_end);
t = t_start:0.01:t_end;
for i = 1:length(t);
if (t(i) >= 0) && (t(i) <= 10);
v(i) = 11.*(t(i).^2) - (5.*t(i));
elseif (t(i) >= 10) && (t(i) <= 20);
v(i) = 1100 - 5.*t(i);
elseif (t(i) >= 20) && (t(i) <= 30);
v(i) = 50.*t(i) + 2*((t(i)-20).^2.5);
elseif (t(i) >= 30) && (t(i) <= 100);
v(i) = 1520.*exp(-0.1.*(t(i)-30));
elseif (t(i) >= -100) && (t(i) <= 0);
v(i) = 0;
end
end
end
#m file
clear all; clc; close all
t_start = input('enter the start time');
t_end = input('enter the end time');
VPieceWise(t_start,t_end)
plot(t,v)
Since your function has two outputs, you should also assign them when calling the function. If you do not do that, only the first output will be put in the ans variable.
So call your function as follows:
clear all; clc; close all
t_start = input('enter the start time');
t_end = input('enter the end time');
[t,v] = VPieceWise(t_start,t_end);
plot(t,v)

Error using == Matrix dimensions must agree. Error in Naivayes (line 21) if (yy{j}==yu{i})

Am totally new to Matlab I need some help, I got some error while running the code
fid = fopen('C:\Users\Oni\Documents\MATLAB\data.csv')';
out = textscan(fid,'%s%s%s%s%s','delimiter',',');
fclose(fid);
num_featureswithclass = size(out,2);
tot_rec = size(out{size(out,2)},1) -1;
for i = 1:tot_rec
yy{i} = out{num_featureswithclass}{i+1};
end
for i = 1: num_featureswithclass
xx{i} = out{i};
end
% Calculation of Prior Probability
yu = unique(yy) %unique class label
nc = length(yu) %number of classes
fy = zeros(nc,1);
num_of_rec_for_each_class = zeros(nc,1);
for i=1:nc
for j =1:tot_rec
if (yy{j}==yu{i})
num_of_rec_for_each_class(i) = num_of_rec_for_each_class(i) +1;
end
end
end
% Likelihood Table
prob_table = zeros(num_featureswithclass-1,10,nc);
for col = 1:num_featureswithclass-1
unique_value = unique(xx{col});
rec_unique_value{col} = unique_value;
for i = 2: length(unique_value)
for j = 2:tot_rec+1
if strcmp (xx{col}{j}, unique_value{i}) == 1 &&
strcmp(xx{num_featureswithclass}{j}, yu{1}) == 1
prob_table(col, i-1,1) = prob_table(col,i-1,1) +1;
end
if strcmp(xx{col}{j}, unique_value{i}) == 1 && strcmp
(xx{num_featureswithclass}{j}, yu{2}) == 1
prob_table(col, i-1,2) = prob_table(col,i-1,2) + 1;
end
end
end
end
prob_table(:,:,1) = prob_table(:,:,1)./num_of_rec_for_each_class(1);
prob_table(:,:,2) = prob_table(:,:,2)./num_of_rec_for_each_class(2);
% The matrix "prob_table" used in the above code is a matrix of
% 4 * 10 * 2 deimension where "4" is the number of attributes in the
% dataset.The number "10" is the possible number of unique value in
% any attribute.In this example,the maximum number was "3". The
% number "2" refer to the number of classes. If we see the values
% present in theprob_table, the understanding will be further enhanced
% Predicting for an unlabeled record**
% Now that we have a naive Bayesian classifier in the form of
% tables, we can use them to predict newly arriving unlabeled
% records. The following code snippet describes the prediction
% process in matlab
A = {'Sunny', 'hot', 'high', 'false'};
A1= find(ismember(rec_unique_value{1},A{1}));
A11 = 1;
A2 = find(ismember(rec_unique_value{2},A{2}));
A21=2;
A3 = find(ismember(rec_unique_value{3}, A{3}));
A31 = 3;
A4 = find(ismember(rec_unique_value{4},A{4}));
A41 = 4;
ProbN = prob_table(A11,A1 - 1,1) *prob_table(A21,A2 - 1,1)*prob_table(A31, A3 - 1,1) *prob_table(A41,A4 - 1,1) *fy(1);
ProbP = prob_table(A11,A1 - 1,2) *prob_table(A21,A2 - 1,2)*prob_table(A31, A3 - 1,2) *prob_table(A41,A4 - 1,2) *fy(2);
if ProbN > ProbP
prediction = 'N'
else
prediction = 'P'
end

matlab generalized cross correlation and ROTH Filter

I have a problem for finding cross correlation of tho signals. No filtere version works but ROTH filter version does not work. Can you tell me the problem please ? Thanks
tho=2;
A=1;
t=-15:0.1:15;
FFTLength=length(t);
x= rect( t+1,tho,A ); % First rectangle signal
plot(t,x)
hold on
y=rect(t-4,tho,A);
plot(t,y,'r')
for i=1:length(x)
x(i)=x(i)+normrnd(0,0.1);
end
figure
plot(t,x,'r')
for i=1:length(y)
y(i)=y(i)+normrnd(0,0.1);
end
figure
plot(t,y,'r')
Rxx = xcorr(x);
Ryy = xcorr(y);
Rxy = xcorr(x,y);
Sxx = fft(Rxx,FFTLength);
Syy = fft(Ryy,FFTLength);
Sxy = fft(Rxy,FFTLength);
X=fft(x,301);
Y=fft(y,301);
%Filtering
H=1;
X2=X.*H;
Y2=Y.*H;
x_t=ifft(X2);
y_t=ifft(Y2);
[correlation,lags] = xcorr(y_t,x_t);
delay = lags(find(correlation==max(correlation)))
disp('Plain time ')
X2=fft(x).*(1./Sxx);
Y2=fft(Y).*(1./Sxx);
x2=real(ifft(X2));
y2=real(ifft(Y2));
[correlation2,lags2] = xcorr(x2,y2);
delay2 = lags2(find(correlation2==max(correlation2)))
disp('ROTH Filter ');
by the way rect is ;
function [ y ] = rect( t,tho,A )
for i = 1 : length(t)
if -(tho / 2) <= t(i) && t(i) <= (tho / 2)
y(i) = A * 1;
else
y(i) = 0;
end
end
end

Matlab trapezoidal Rule

I created a fas.m script, but I'm getting wrong result.
FUNCTION
function [fnc2] = fas(x)
if x>=0 && x<1
fnc2 = x^3;
elseif x>=1 && x<2
fnc2 = 2-((x^2)/5);
elseif x>2
fnc2 = x^2+x;
elseif x<0
fprintf('x is smaller than 0, function is not defined');
end
TRAPEZOIDAL RULE SUM
clear
clc
h=0.05;
x=0.05;
x0=0;
xn=3;
while x<=2.95
fas(x);
I=0.025*(fas(x0)+2*fas(x)+fas(x0));
x=x+h;
end
Trapezoidal rule is,
so,
h = 0.05;
x = 0;
I = 0;
while x < 3
I = I + h * (fas(x) + fas(x + h)) / 2;
x = x + h;
end
disp(I);
you will get I = 11.3664 while the actual value of I is 10.3667.

Matlab wont quit using the cross sign in the waitbar when there is an out of bound exception for the image

The value for the columns is 51 and 50, but when we use anything more than that the waitbar freezes due to index out of bound exception since its a large image and it wont fit in there, so the matlab dosent shut using the waitbar or anything. Need a way to shut the matlab when it encounters any error.
h = waitbar(0,'Progress','Name','Calculating Feature Heights...',...
'CreateCancelBtn','setappdata(gcbf,''canceling'',1)');
setappdata(h,'canceling',0); %initiallizes waitbar
s1 = size(A);
s2 = size(B);
if (s1(1) < s2(1))
n = s1(1);
else
n = s2(1); % ensures that bounds of i are within the bounds of both images
end
for i = 21:1:n % sets bounds for rows
if getappdata(h,'canceling') %checks for user pushing the cancel button on the waitbar
break
end
waitbar(i/(n-1),h) %progress bar
for j = 61:1:(m-60) % sets bounds for columns
if A(i,j) == A(i,j-1) %if adjacent pixels are the same,
Z(i,j) = Z(i,j-1); %they have the same height
disp(i,j) = disp(i,j-l);
elseif A((i), j) == B(i, j) && A(i,j) ~= A(i,j-1) && A(i,j-1) == B(i,j-1)
Z(i,j) = Z0; %condiions for pixels/features in the 'focal plane'
disp(i,j) = 0;
else
for l = 1:1:20 %sets scan range in rows for disparity
for k = 1:1:60 %sets disparity scan range in cols
if (A(i,j) == B(i-l, j-k) && B(i-l, j-k-1) == B(i-l, j-k))
Z(i,j) = Z(i-l,(j-k-1)); %allows for multipixel features
disp(i,j) = disp(i-l,(j-k-1));
break
elseif (A(i, j) == B(i-l, j-k) && B(i-l, j-k-1) ~= B(i-l, j-k))
xA = [i j];
xB = [i-l j-k];
d = xB-xA;
Z(i,j) = Z0 - (fl*shift)/sqrt((d(1)^2)+(d(2)^2));
disp(i,j) = sqrt((d(1)^2)+(d(2)^2));
break
elseif (A(i,j) == B(i-l, j+k) && B(i-l, j+k-1) == B(i-l, j+k))
Z(i,j) = Z(i-l,(j+k-1));
disp(i,j) = disp(i-l,(j+k-1));
break
elseif (A(i, j) == B(i-l, j+k) && B(i-l, j+k-1) ~= B(i-l, j+k))
xA = [i j];
xB = [i-l j+k];
d = xB-xA;
Z(i,j) = Z0 - (fl*shift)/sqrt((d(1)^2)+(d(2)^2));
disp(i,j) = sqrt((d(1)^2)+(d(2)^2));
break
else
continue
end
end
end
end
end
end
delete(h)
Use a try/catch block.
try
% whatever that might error
catch
delete(h)
end