Step functions/Heaviside Functions MATLAB error - matlab

So im trying to plot f(t) which is a piecewise function using heaviside and ezplot functions in MATLAB. Now im not very familiar with MATLAB at all. If anyone knows why im getting this error it would be helpful.
f = '12+(-2t+8)*heaviside(t-2)+(2t-12)*heaviside(t-6)'
f = 12+(-2t+8)*heaviside(t-2)+(2t-12)*heaviside(t-6)
ezplot(f)
Error using inlineeval (line 15) Error in inline expression ==> 12+(-2t+8).*heaviside(t-2)+(2t-12).*heaviside(t-6) Error: Unexpected MATLAB expression.
Error in inline/feval (line 34)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 52)
z = feval(f,x(1));
Error in ezplot>ezplot1 (line 469)
[y, f, loopflag] = ezplotfeval(f, x);
Error in ezplot (line 145)
[hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});

You need to tell Matlab to multiply explicitly, use 2*t instead of 2t.
syms t real
f = 12+(-2*t+8)*heaviside(t-2)+(2*t-12)*heaviside(t-6)
ezplot(f,[0 10])

Related

Using lsqcurvefit on a matrix

I trying to use lsqcurvefit on matrix with Pi groups.
I'm VERY new to using Matlab but I have tried to do this:
(xdata=30x5, ydata = 30x1)
xdata=[PI2, PI3, PI4, PI5, PI6]'
ydata=PI1
myfun = #(x,xdata) x(1).*(xdata(:,1).^(x(2))).*(xdata(:,2).^(x(3))).*(xdata(:,3).^(x(4))).*(xdata(:,4).^(x(5))).*(xdata(:,5).^(x(6)))+x(7)
x0 = [0; 2.5];
[x] = lsqcurvefit(myfun,x0,xdata,ydata)
I get following errors:
Index exceeds the number of array elements (2).
Error in
AnalysisBasic>#(x,xdata)x(1).(xdata(:,1).^(x(2))).(xdata(:,2).^(x(3))).(xdata(:,3).^(x(4))).(xdata(:,4).^(x(5))).(xdata(:,5).^(x(6)))+x(7)
(line 83)
myfun = #(x,xdata)
x(1).(xdata(:,1).^(x(2))).(xdata(:,2).^(x(3))).(xdata(:,3).^(x(4))).(xdata(:,4).^(x(5))).(xdata(:,5).^(x(6)))+x(7)
Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in AnalysisBasic (line 86)
[x] = lsqcurvefit(myfun,x0,xdata,ydata)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
What do I do wrong?
Equation

Sequential numerical integration in MATLAB

I am trying to write the program in MATLAB to perform sequential integration. First I define the first integral n_array_exact{1}() and save it as function handle in the array. This first integral becomes a function of x only after the integration. As a next step I use this function to calculate next double integral (call it n_array_exact{2}(x)) w.r.t mu1 and s. Before substituting n_array_exact{1}(x) to calculate n_array_exact{2}(x), I replace x in n_array_exact{1}(x) to a function called xp(x, mu1,s) instead.
% Ordinary integration
n_array_exact{1} = #(x) integral(#(mu) exp((x.*mu)), -1,1)/2;
xp2= #(x,mu1,s) sqrt(x*x +s*s +2*x*mu1*s);
for i=2:3
n_array_exact{i} = #(x) integral2(#(mu1,s) exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s)), 0,1,-1,1);
end
n_array_exact{2}(0.05)
%n_array_exact{3}(0.05)
%n_array_exact{4}(0.05)
After performing the integration and trying to evaluate n_array_exact{2}0.05) I got the following errors:
Error using .*
Matrix dimensions must agree.
Error in #(mu)exp((x.*mu))
Error in integralCalc/iterateScalarValued (line 315)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in #(x)integral(#(mu)exp((x.*mu)),-1,1)/2
Error in #(mu1,s)exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s))
Error in integral2Calc>integral2t/tensor (line 229)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 10)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in #(x)integral2(#(mu1,s)exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s)),0,1,-1,1)
Error in test (line 10)
n_array_exact{2}(0.05)

3D Spherical Plot in Matlab with Derivatives and Bessel Functions

I want to plot the following function B(r,theta,phi) in spherical coordinates.
Bo = 1;
a = 1; % m
lambda = 1; % m^-1
syms r theta;
Br = 2*Bo*(a/r)*besselj(1,lambda*r)*cos(theta)
Bth = -Bo*(a/r)*diff(r*besselj(1,lambda*r),r)*sin(theta)
Bph = lambda*a*Bo*besselj(1,lambda*r)*sin(theta)
[Bx,By,Bz]=sph2cart(Bth,Bph,Br);
x=linspace(0,a);
y=linspace(0,a);
z=linspace(0,a);
quiver3(x,y,z,Bx,By,Bz)
When I run the above code, I receive the following warning and error.
Warning: Using only the real component of complex data.
> In getRealData (line 14)
In quiver3parseargs (line 87)
In quiver3HGUsingMATLABClasses (line 41)
In quiver3 (line 41)
In taylor_state (line 19)
Error using quiver3 (line 43)
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
Error in taylor_state (line 19)
quiver3(x,y,z,Bx,By,Bz)

How do I create function handle from cfit, multiply with other function handle and integrate the term?

I try to get an integral of two function handles in Matlab. The first function handle would be a weibull probability density function and the second function handle is based on a cfit I created with linear interpolation of single points.
x = 0:0.1:35;
fun1 = #(x) wblpdf(x,weibullAlpha,weibullBeta);
fun2 = #(x) feval(cfitObject,x);
fun3 = #(x) (fun(x).*fun2(x));
y = integral(fun3,0,35); % Using quad(fun3,0,35) doesn't work either.
I receive the following error:
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is an array-valued integrand,
set the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in test (line 7) % "test" is the name of the script file.
y = integral(fun3,0,35);
The problem must have to do something with "fun2" since the code works just fine with e.g.
fun2 = x.^2;
Note: if I plot fun2 with the cfitObject I don't get an error. It's also possible to integrate the function using quad().
x = 0:0.1:35;
fun2 = #(x) feval(cfitObject,x);
y = quad(fun2,0,35);
plot(x, fun2(x))
Any help is greatly appreciated!
Your code seems to be ok. Probably the problem is that fun2 cannot take vectorized input, it could be resolved by modifying fun2 (cfitObject) to be able to handle vector input or telling the software that the function in the integral is array valued:
y = integral(fun3, 0, 35, 'ArrayValued', 1);

How to plot x+y in MatLab using ezplot

I tried ezplot in MatLab
ezplot('1.395x-1.935y+16.65')
but got the following errors:
Error using inlineeval (line 14)
Error in inline expression ==> 1.395x-1.935y+16.65
Error: Unexpected MATLAB expression.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 51)
z = feval(f,x(1));
Error in ezplot>ezplot1 (line 468)
[y, f, loopflag] = ezplotfeval(f, x);
Error in ezplot (line 144)
[hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});
I think you forgot to put a multiplication sign (*). Try this:
ezplot('1.395*x-1.935*y+16.65')