How do i find the integral of the movmean function on the top graph - matlab

How do i find the integral of the movmean function on the top graph
The intz doesnt work, and im not so sure what the trap gives me:
subplot(3,1,1);
Fz1=detrend(Fz(500:1200000));
plot(Fz1,'Color','k');
hold on
M1 = movmean(Fz(500:1200000),[2000,2000]);
M11=detrend(M1);
plot(M11,'Color','r')
trapz(M11)
intz = int(M11)
this line intz = int(M11)throwing an exception. Please tell me what is wrong on here

You are confusing your commands. The trapz is the command that is doing the integration using the trapezoidal integration method. The command int does not do integration; it creates variables of integer type given integer-valued floating-point (a.k.a. double) variables.
Most likely you are seeing this kind of error
M11 = [1.2 3.4]; % as an example
int(M11)
Undefined function 'int' for input arguments of type 'double'.
because your data in M11 contains non-integer values. In short int is not necessary for integration; just use trapz. See help trapz in MATLAB for details on using this command.

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

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.

Double inverting symbolic

When I ran a simple exercise of integration of defined functions as below,
clear all;
syms z tau;
deltav=tau^(1/(3*z))-tau^(1/(4*z));
deltax=1/(0.5+12*z)*(tau^(1/(3*z))-tau^(1/(4*z)));
a=1;
b=9;
tau=0.5;
mu_vx=int(deltav*deltax,a,b);
mu_x2=int(deltax^2,a,b);
ratio=double(mu_vx/mu_x2);
I got error message from MATLAB by saying "??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into
a double array.If the input expression contains a symbolic variable, use the VPA
function instead."
So I plug in the expression of "deltav" and "deltax" in to integration, and run
clear all;
syms z tau;
deltav=tau^(1/(3*z))-tau^(1/(4*z));
deltax=1/(0.5+12*z)*(tau^(1/(3*z))-tau^(1/(4*z)));
a=1;
b=9;
tau=0.5;
mu_vx=int((tau^(1/(3*z))-tau^(1/(4*z)))*1/(0.5+12*z)*(tau^(1/(3*z))- tau^(1/(4*z))),a,b);
mu_x2=int((1/(0.5+12*z)*(tau^(1/(3*z))-tau^(1/(4*z))))^2,a,b);
ratio=double(mu_vx/mu_x2)
It works this time. I wonder how I should make the first way work without plugging the long expression. Thank you.
Sometimes a solve is necessary to find a explicit solution
mu_vx=int(solve(deltav*deltax),a,b);
mu_x2=int(solve(deltax^2),a,b);
ratio=double(mu_vx/mu_x2);

Propose Fixed-Point Data Types Using an Instrumented Mex Function

This is my matlab function:
function [mask] = computeMask (I, threshold, win_var, borderx, bordery)
coder.extrinsic('imfilter');
%-- compute Variances of the RGB and IR patches
mean_I = imfilter(I,ones(win_var)/win_var/win_var,'same','symmetric','conv');
mean2_I = imfilter(I.^2,ones(win_var)/win_var/win_var,'same','symmetric','conv');
std_I= real(sqrt(mean2_I-mean_I.^2)+1e-5);
mask= std_I>threshold;
% add the border of the image to the mask
% (filters are not complete at the border)
mask(1:((bordery-1)/2),:)=0;
mask((end-(bordery-1)/2):end,:)=0;
mask(:,1:((borderx-1)/2))=0;
mask(:,(end-(borderx-1)/2):end)=0;
% add the area where the vignetting is too strong
Cy=floor(size(mask,1)/2);
Cx=floor(size(mask,2)/2);
[x,y]=meshgrid(1:size(mask,2),1:size(mask,1));
mask(((x-Cx).^2+(y-Cy).^2)>8e4)=0;
%mask(((x-Cx).^2+(y-Cy).^2)>1.3e4)=0;
return
When trying to build Build "Instrumented MEX Function" I get these errors: ??* ? Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.*
Error in ==> computeMask Line: 21 Column: 18: std_I= real(sqrt(mean2_I-mean_I.^2)+1e-5);
How to fix these problems? please help me !!! Thank you very much
Since you declared imfilter as extrinsic, the generated code will call into MATLAB to run this function. This will result in an mXArray type. To help coder convert mxArray type to a native type you should initialize the outputs of imfilter before calling imfilter. For example, if the output of imfilter is of same type and size as its input I, then use
mean_I = I;
mean_I = imfilter(I,ones(win_var)/win_var/win_var,'same','symmetric','conv');
Note that imfilter still does not generate any code. imfilter is still called in MATLAB.
You can see documentation for this at http://www.mathworks.com/help/fixedpoint/ug/calling-matlab-functions.html in section "Converting mxArrays to Known Types".

Cannot integrate the function. double to sym

I'm trying to integrate the function. I'm getting the error
Undefined function 'int' for input arguments of type 'double'.
Here is my code:
P = #(m,sigma,t,C) (normcdf((C-m/sigma)/sqrt(t),0,1) - exp(2*C*m/sigma)*normcdf((-C-m/sigma)/sqrt(t),0,1));
Pr = #(m1,m2,sigma_1, sigma_2,t,C) (P(m1,sigma_1,t,C)*P(m2,sigma_2,t,C));
P_S = #(m1,m2,sigma_1,sigma_2,C) (1 - int(Pr(m1,m2,sigma_1,sigma_2,t,C), t, 0, inf));
What am I doing wrong and how to integrate this function?
normcdf is used for floating point calculations and does not support symbolic input. You need to write your own. Luckily that's pretty easy. Try replacing normcdf with this:
normcdf_sym = #(x,mu,sig) (1./(sig*sqrt(2*sym('pi'))))*int(exp(-(t-mu).^2./(2*sig.^2)),t,-Inf,x);
Or with this, which is is equivalent to the above:
normcdf_sym = #(x,mu,sig) (1+erf((x-mu)./(sig*sqrt(2))))/2;
Also, you'll likely want to define your symbolic variables as real: syms m sigma t C real;. Or use the assume and assumeAlso functions.
All this is assuming you want to use symbolic integration in the first place, as opposed to numeric integration.