Writing cos functions with two variables in MATLAB - matlab

How do I write the following function in MATLAB:
f(x)=cos h (x^2)
I have been trying this:
function y=f01(x,h)
y=cos(h).(x.^2);
function y=f01(x,h)
y=cos(h)*(x.^2);7
function y=f01(x,h)
y=cos(h)(x.^2);
And many more instances. Nothing works. I can't find similar examples in the cos function documentation.

I found out that the equation can be written as follows:
function y=f01(x,h)
y=cos(h*(x.^2));

Related

using ilaplace in conjunction with rltool

Using Matlab, I have the following code:
s = tf('s')
K=0.5;
H= 1/(s*(s+2));
Hcl=feedback(K*H,1)
ilaplace(Hcl)
rltool(H)
I want to get the inverse laplace transform of the unity closed-loop system.
rltool(H) generates automatically the unity closed-loop system. That's why I pass the open-loop system as an argument.
When I run this code, Matlab gives an error about ilaplace:
"Check for incorrect argument data type or missing argument in call to function 'ilaplace'."
Can someone help me how to use ilaplace and rltool concurrently
I have found a solution for this problem.
syms s;
K=1/2;
H= 1/(s*(s+2))
Hcl=simplify(K*H/(1+K*H))
P=poles(Hcl)
ilaplace(Hcl)
H = syms2tf(Hcl)
rltool(H)
ilaplace works with symbolic expressions and not with a transfer function. By converting the symbolic expression to a transfer function midway of the code, I can use both functions in the same code.
Notice I've added the function simplify and changed the beginning from s=tf('s') to syms s
The function syms2tf() can be found here

How can I create multiple inputs for a matlab function block?

I want to restrict the variable that I use as input for a matlab function block, it must be only able to increase.
To achieve that i have tried to compare the variable and the previous sample of it in a matlab function, but i don't know how to create two inputs. To solve that i've tried to use a mux, but then i get an error. And google doesn't give me an explanation how to use a mux signal as input for a matlab function.
So that leaves me here with this low-level question.
Thanks in advance for your help and time. Cheers.
To use multiple variables in a function, you need to modify your function declaration at the first line of your function. The reference syntax is:
function [y1,...,yN] = myfun(x1,...,xM)
where x1 through xM are inputs. Your declaration with two inputs might look something like:
function [returnValue] = hasIncreased(previousSample, variable)
See the Matlab Function Documentation for more information.

using ode45 to solve y'=y adnd y'=t in Matlab

I first defined functions for dy/dt=y and dy/dt=t:
function dy=d(y):
dy=y
end
function ddy=dd(t):
ddy=t
end
And then I used ode45, respectively:
[t,y]=ode45('d',[1 10],1)
[t,y]=ode45('dd',[1 10],1)
which returns the following error: Error using d
Too many input arguments.
My question is:
Where did I go wrong?
How does Matlab know whether y or t is the independent variable? When I define the first function, it could be reasonably interpreted as dt/dy=y instead of dy/dt=y. Is there a built-in convention for defining functions?
First things first: the docs on ode45 are on the mathworks website, or you can get them from the console by entering help ode45.
The function you pass in needs to take two variables, y then t. As you noticed, with just one it would be impossible to distinguish a function of only y from a function of only t. The first argument has to be the independent, the second is the dependent.
Try defining your function as dy = d(t, y) and ddy = dd(t, y) with the same bodies.
one other note, while using a string representing the function name should work, you can use #d and #dd to reference the functions directly.

Where is the source code for the function refine(..) used in Matlab's estimateCameraParameters.m?

At line 486 in estimateCameraParameters.m the refine function is called as shown below. Where is the source code of that function ?
I have searched all Matlab scripts but could not find it.
There is no documentation on this function either.
Any idea where to look for it ?
Where is refine hiding ?
errors = refine(stereoParams, imagePoints1(:, :, pairsUsed), ...
imagePoints2(:, :, pairsUsed), shouldComputeErrors);
At line 303 there is a short description of this function :
% refine the initial estimate and compute distortion coefficients using
% non-linear least squares minimization
errors = refine(cameraParams, imagePoints, shouldComputeErrors);
Found it: it's in stereoParameters.m
I just needed to start to step through estimateCameraParameters.m with the debugger.
Additional explanation:
http://se.mathworks.com/help/matlab/matlab_oop/ordinary-methods.html
Either of the following statements is correct syntax for calling a
method where obj is an object of the class defining the compute
method:
obj.compute(inc)
compute(obj,inc)
So refine() is a method not a standalone function.
I did not know that it is possible to call a method on an object using method(object) syntax. This was confusing me and I was thinking that refine() is a standalone method not defined within a class.

MATLAB- Renaming issue of heaviside.m

1) int(( heaviside(sym('t')) ),0,0.5 ) works perfectly but when I edit the in-built MATLAB file heaviside.m and rename it as my_heaviside.m and then call it in the command window as
int(( my_heaviside(sym('t')) ),0,0.5 ) it shows error.
2) How to write the following piecewise function with the help of heaviside:
f(x)= 1, 2<=x <3 & 0 elsewhere. Note that at x=2, I require f(x)= 1 and at x=3, I require f(x)= 0.
3) I want that the f(x) in point no. 2 should be defined in such a way that it can be symbolically integrate like in point 1.
You should try to change the function name inside my_heaviside.m file from heaviside to my_heaviside.
If you use a breakpoint in the heaviside function, then you will see that Matlab never actually goes into the function when evaluating heaviside(t). Why? What should be the output of heaviside(t)? It is supposed to be heaviside(t)! What it actually does is it goes to the method heaviside inside of the sym class definition, the output of that function is just mupadmex('symobj::map',t.s,'heaviside') which is just another name for the heaviside of t (here s is a private property of a sym object).
Also when Matlab tries to integrate the heaviside function, it cannot integrate it from first principles by looking at the structure of the mfile, but it uses theorems to evaluate expressions containing it; thus one cannot hope to edit the actual mupad file for heaviside and expect Matlab to find the correct integral.