MatLab Plotting Function Matrix Dimension Error - matlab

I have a simple function written
function[] = myfun(p,q)
fminbnd(#(x)myfun1(q,p,b),0,1)
where myfun1's output is from the function quad.
How do I plot myfun? I've tried fplot(#(x)myfun(1,x),0,1) but this gives me a matrix dimensions must agree error...

Your question does not contain enough information to identify exactly where the problem lies, but one issue is certainly that myfun does not return any output. What should fplot plot, if there is nothing returned by the function?
Try
function out = myfun(p,q)
%# you may want to define b here
out = fminbnd(#(x)myfun1(q,p,b),0,1);
If that doesn't fix the issue, turn on the debugging mode by typing dbstop if error at the command line. This will show where exactly the error occurs, and allow you to inspect the variables for correct shape.

Related

How to get the zeros of the given equation using fzero in MATLAB?

I have the following function that I wish to solve using fzero:
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)}
Here, C, T_m, T_i, and L_f are all input by the user.
On trying to solve using fzero, MATLAB gives the following error.
Undefined function or variable 'X'.
(where X are the variables stated above)
This error is understandable. But is there a way around it? How do I solve this?
This is answered to the best of my understanding after reading your question as it's not really clear what you are exactly trying and what you want exactly.
Posting the exact lines of code helps a big deal in understanding(as clean as possible, remove clutter). If then the output that matlab gives is added it becomes a whole lot easier to make sure we answer your question properly and it allows us to try it out. Usually it's a good idea to give some example values for data that is to be entered by the user anyway.
First of to make it a function it either needs a handle.
Or if you have it saved it as a matlab file you generally do not want other inputs in your m file then the variable.
So,
function [out]=yourfun(in)
constants=your values; %you can set a input or inputdlg to get a value from the user
out= something something, your lambda thingy probably; %this is the equation/function you're solving for
end
Now since that is not all that convenient I suggest the following
%declare or get your constants here, above the function makes it easier
syms lambda
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)};
hf=matlabFunction(f); %this way matlab automatically converts it to a function handle, alternatively put #(lambda) in front
fzero(hf,x0)
Also this matlab page might help you as well ;)

Running MATLAB function without defining all parameters

I have a Matlab function
PlotSubbands(imfx(:,1),wx,3,3,j,j,1);ylabel('Subband');
from TQWT toolbox. eeweb.poly.edu/iselesni/TQWT. When I execute, the function plots 'j' number of plots. (I have not included full code). The function plots the input signal, which is in this case imfx(:,1) for every subplot. And this what I don't want. I tried removing it from the parameters but I got error, 'not enough input arguments'. This is because in the function the first input signal parameters is defined and used. I cannot remove it from there. Appreciate your inputs on the same. Thank you.
The function PlotSubbands include following lines
if isreal(x)
plot((0:N-1)/fs,x/(2*max(abs(x))),'b')
else
plot((0:N-1)/fs, real(x)/(2*max(abs(x))), 'black')
The repeated plotting of input signal is done from these two line. Commenting these lines can solve the problem (If array x does not include imaginary part, then better to comment these lines). However, the to determine the real array, isreal(x) is needed further in the function. So, I defined the value of x here and it solved the repeated plotting issue.

Using Octave arrayfun with a string argument?

I'm using arrayfun to plot the result of a custom function, which does some logic, look-ups, and calculations. My original call looked similar to this:
plot(x, arrayfun(#Q, x.^2, someNumericVariable));
This worked great. However, in addition to the someNumericVariable parameter, I also wanted to add another parameter, someStringVariable, so I changed it to this:
plot(x, arrayfun(#Q, x.^2, someNumericVariable, someStringVariable));
However, when trying to use this, I get an error:
error: arrayfun: dimensions mismatch
I'm guessing that this happens due to this line in the GNU Octave documentation:
If given more than one array input argument then all input arguments must have the same sizes
(https://www.gnu.org/software/octave/doc/interpreter/Function-Application.html)
So I assume that the string I'm trying to pass is being treated as an array, which has different dimensions than the numeric constant value?
If this is so, are there any workarounds that I can do while keeping the code syntactically concise?
One workaround that would work in MATLAB would be:
plot(x, arrayfun(#(u,v) Q(u,v,someStringVariable), x.^2, someNumericVariable));

MATLAB: findpeaks function

I'm using MATLAB 2013a and trying to find peak points of my data. When I tried code example given in
Find Peaks with Minimum Separation
I am getting the following error:
Error using uddpvparse (line 122)
Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84)
hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59)
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
I tried simple x and y vectors and got the same error. What can be the problem?
I have the same problem as you (R2013a on OSX) with the example by the Mathworks. For some reason it seems we can't use findpeaks with the x-and y-data as input arguments, We need to call the function with the y data and use the [peaks,locations] output to get the peaks/plot them.
It looks like in R2014b they changed some stuff about findpeaks that does not work with older versions...like calling the function with not output argument in R2014b plots the data/peaks without any additional step...but it does not for earlier versions.
Anyhow here is a way to workaround the problem. Call findpeaks with a single input argument (y data that is, you can use property/value pairs as well) and use the indices (locations) to show the peaks:
clc
clear
load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
[peaks, locations] = findpeaks(avSpots)
plot(year,avSpots)
hold on
scatter(year(locations),avSpots(locations),40,'filled')
hold off
Output:
It might be worthwhile to contact The Mathworks about this. Hope that helps!

Using MATLAB svmtrain

This is my first Matlab program.
I'm trying to use svmtrain and svmclassify with custom kernel.
Assume my kernel is regular inner product.
How should I write it?
I did:
function [K] = mykernel(U, V)
for i=size(U,1)
for j=size(V,1)
K(i,j) = dot(U(i,:),V(j,:));
end
end
return
end
and then in the command window:
x=randn(1000,10);
w=rand(1,10);
y=sign(x*w');
a=svmtrain(x,y,'kernel_function',mykernel);
and I get:
Error using mykernel (line 2)
Not enough input arguments.
Maybe one has a trick to do it without loops, something like U*V', it'll be nice to know this
trick, but i need to do it in loop, since i'm going to change the inner product to some more complicated stuff.
I also didn't really understand what are those U,V, and I didn't really get what this function
should return (is it the Gram matrix?)
Thanks for your help!!
--- EDIT:
I did the following:
function [K] = mink(U, V)
for i=1:size(U,1)
for j=1:size(V,1)
K(i,j) = min(exp(-dot(U(i,:),U(j,:))),exp(-dot(V(i,:),V(j,:))));
end
end
return
end
>>x=randn(100,10);
>>w=rand(1,10);
>>y=sign(x*w');
>>a=svmtrain(x,y,'kernel_function',#mink);
>>svmclassify(a, x)
Error using svmclassify (line 114)
An error was encountered during classification.
Attempted to access U(89,:); index out of bounds because size(U)=[88,10].
so now svmtrain works but svmclassify complains about size mismath (how did it get 88??)
In order to pass a function, you need to use the # symbol. This is shown in the docs, which quote:
#kfun — Function handle to a kernel function. A kernel function must be of the form
Bottom line, this will work.
a=svmtrain(x,y,'kernel_function',#mykernel);