MatLab GUI Troubles: Calculating an Integral - matlab

I have created a matlab GUI in which a user selects a variable to integrate with respect to, inputs the equation, and lower and upper limits. When my code goes to calculate the integral on pushbutton Callback, I get an error I don't understand.
This is the line of code causing the error:
i1 = int( eval(get(handles.edit1,'string')),
(handles.respectvar),
get(handles.edit3),
get(handles.edit2)
);
%respactvar is the user-selected variable, and edit3 and edit2 are the lower and upper limits.
And this is the error message:
Error using sym>tomupad (line 1135)
Conversion to 'sym' from 'struct' is not possible.
Error in sym (line 151)
S.s = tomupad(x);
Error in sym/int (line 142)
b = sym(b);
Error in projectCALC>pushbutton1_Callback (line 376)
i1=int(eval(get(handles.edit1,'string')),(handles.respectvar),get(handles.edit3),get(handles.edit2));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in projectCALC (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)projectCALC('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Thank you!

There are multiple problems with your code.
The first problem, as #excaza noted, is that you forgot to get the string property of the last two edit boxes, while you did this for the first one.
The second problem is that eval is very inefficient, and, in your case, very unsafe. What if somebody wrote a system() call into the editbox, erasing your hard drive?
The third problem is that even with eval or str2func, int requires its first argument to be a symbolic expression. This is a good thing: you don't actually have to use eval, you just have to convert your first string to a sym.
The fourth problem is that handles.respectvar also seems to be a GUI object, so you might need to get() some property for it in order to be able to use it as an integration variable.
Assuming that my suspicion is correct, and your edit1 contains a string like '3*x+y', edit2 and edit3 are limits such as '1' and '3', and handles.respectvar has a property that evaluates to 'x', then you should be calling
formula = sym(get(handles.edit1,'string'));
variable = handles.respectvar; %// more likely: get(handles.respectvar,...)
lim1 = str2double(get(handles.edit3,'string'));
lim2 = str2double(get(handles.edit2,'string'));
i1 = int(formula,variable,lim1,lim2);
A few notes:
Don't be afraid of using temporary variables where it helps in readability.
I converted the formula from sym to string, otherwise you get a warning or an error (depending on MATLAB version)
I left the order of lim1 <-> edit3, lim2 <-> edit2. Make sure this is indeed what you want, and orders are not reversed.
I converted the integration limits to double, but it seems that sym/int will also accept string input as the limit variables, so this is mostly for clarity.

Related

About symbolic MATLAB

What is wrong with the following code?
clear all
syms x y a ;
u=2*x*y;
v=a^2+x^2-y^2;
diff=diff(u,'x',1)+diff(v,'y',1);
if diff==0
disp('Liquid motion is possible.')
disp('Stream function exists.')
else
disp('Liquid motion is not possible.')
disp('Stream function does not exist.')
end
diff2=diff(v,'x',1)-diff(u,'y',1);
if diff2==0
disp('Velocity potential exists.')
else
disp('Velocity potential does not exist.')
end
This comes in the command window when I run the above.
Liquid motion is possible.
Stream function exists.
Error using sym/subsindex (line 672)
Invalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM
object. When indexing, the input must be numeric, logical or ':'.
Error in sym>privformat (line 1502)
x = subsindex(x)+1;
Error in sym/subsref (line 694)
[inds{k},refs{k}] = privformat(inds{k});
Error in q7 (line 17)
diff2=diff(v,'x',1)-diff(u,'y',1);
But if I rewrite(redefine) the symbolic variables after the first if construct, it runs fine. Also if I cancel the first if construct, it runs.
I would avoid to overwrite a reserved name, so instead of
diff=diff(u,'x',1)+diff(v,'y',1);
I would suggest
derFcn = diff(u,'x',1)+diff(v,'y',1);
This triggers the second error;
diff2=diff(v,'x',1)-diff(u,'y',1);
at this point diff is your diff value (which, incidentally is 0) so it is equivalent to write
0(v,'x',1)
which, of course, will not compile and it is not what you meant.
So, please, make the substitution (and update your if statements accordingly).

Error using strcmp within fittype

I just received a script from a colleague whose first line is this:
VonMisesModel=fittype(#(A,k,base,peakloc,x) (base + A*exp(k*cos((pi/180)*(x-peakloc)))/(2*besseli(0,k)*pi)),'independent','x');
He's done a good deal of processing with this script, so I'd imagine this line of code works for him.
However, when I call it, I get the following error:
>> VonMisesModel=fittype(#(A,k,base,peakloc,x) (base + A*exp(k*cos((pi/180)*(x-peakloc)))/(2*besseli(0,k)*pi)),'independent','x');
Error using strcmp
The number of rows of the string matrix must match the number of elements in the cell.
Error in cell/ismember (line 38)
match = strcmp(a,b);
Error in fittype>iTestAnonymousFunctionArgumentOrder (line 786)
[~, coefficientLocations] = ismember( obj.coeff, arguments );
Error in fittype>iCreateFromAnonymousFunction (line 533)
iTestAnonymousFunctionArgumentOrder( obj, theFcn )
Error in fittype>iCreateFittype (line 355)
obj = iCreateFromAnonymousFunction( obj, varargin{:} );
Error in fittype (line 328)
obj = iCreateFittype( obj, varargin{:} );
I'd like to reiterate that this is the first line in a stand-alone script. No processing is meant to occur before this line is called.
The error does not seem intuitive to me at all. My guesses at what might have caused it have not turned up a solution. Does anyone know why this error was raised?
Edit
The definition of the fittype function appears to have changed over time.
The error in the question is raised when I use versions of Matlab newer than matlab-2012a.
However, the error is not raised when I use matlab-2012a.
My pre-2012 matlab distributions do not contain the function fittype at all.
In order for other functions in this script to work, I need to get this call to fittype to work with new(er) versions of Matlab. If you can, please let me know how to rearrange the call to prevent the error.

Error regarding inlineeval in MATLAB

As part of a group project we have a system of 2 non linear differential equations and we have to draw the S=S(t) , I=I(t) graphic using the midpoint method.
And I'm getting the following error when trying to insert the matrix with the corresponding differential equations:
"Error in inline expression ==> matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]])
Undefined function 'matrix' for input arguments of type 'double'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);"
The code I have done is the following:
syms I S
u=[S;I];
F=[-0.001*S*I;0.001*S*I-0.3*I];
F1=inline(char(F),'I','S');
h=100; %Valores aleatórios
T=100000;
ni=(T/h);
u0=[799;1];
f=zeros(1,2);
k=zeros(1,2);
i=1;
while i<=ni
f(1)=F1(u0(1));
f(2)=F1(u0(2));
dx=h*f;
k(1)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
k(2)=F1((u0(1)+h*(1/2)),(u0(2)+h*(1/2)));
u1=u0+h*k;
disp('i:'),disp(i)
disp('u= '),disp(u1)
u0=u1;
i=i+1;
end
I'm new to this so the algorithm it's very likely to be wrong but if someone could help me with that error I'd apreciate it. Thank you!
The problem that specifically creates the error is that you are putting two symbolic functions into a matrix and then calling char (which outputs matrix([[-(IS)/1000], [(IS)/1000 - (3*I)/10]]) rather than converting nicely to string).
The secondary problem is that you are trying to pass two functions simultaneously to inline. inline creates a single function from a string (and using anonymous functions instead of inline is preferred anyway). You cannot put multiple functions in it.
You don't need sym here. In fact, avoid it (more trouble than it's worth) if you don't need to manipulate the equations at all. A common method is to create a cell array:
F{1} = #(I,S) -0.001*S*I;
F{2} = #(I,S) 0.001*S*I-0.3*I;
You can then pass in I and S as so:
F{1}(500,500)
Note that both your functions include both I and S, so they are always necessary. Reconsider what you were expecting when passing only one variable like this: f(1)=F1(u0(1));, because that will also give an error.

Matlab: nest fzero in fminsearch

I am trying to minimize with respect to a variable "y" a function that contains a parameter which must be calculated as a solution of an equality that contains "y" as well (say, y=-3; in my complete problem it is an equation with no analytic closed form solution, so I really need fzero).
Because of this, I include the fzero function in the argument of fminsearch:
fminsearch( #(y) 10*fzero(#(y) y+3, 0)) ;
I get the error:
Error using fminsearch (line 85)
The input to FMINSEARCH should be either a structure with
valid fields or consist of at least two arguments.
I obviously get the same error with:
f = fzero(#(y) y+3, 0);
fminsearch(#(y) 10*f);
Apparently the problem is that I cannot "nest" a fzero inside fminsearch.
Any idea about how to turn around this problem?
If you read the error message you got and look at the documentation of fminsearch you'll see that you need to call it with two input arguments. You call it with only one.
fminsearch( #(y) 10*fzero(#(x) x+3, 0), 0 )

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);