Plot Integral with variable limits in Matlab - matlab

I'm having some trouble trying to solve and plot an integral in matlab. In fact, I know that if a solve one, I'll solve all the integrals that I need now.
I have plot in x axis a value of a variable "d" and in y axis the value of a integral of a normalized gaussian function from -inf to ((40*log10(d)-112)/36) and I'm not finding out a way to do it correctly. D is between 0 and 1600
Can anybody here please help me?

In Matlab you can use the integral-function to evaluate integrals:
q = integral(fun,xmin,xmax)
fun needs to be a function handle, also called function functions, like these two examples:
square = #(x) x.^2;
plusone = #(x) x+1;

Related

compute definite integral in Matlab

i have the data below
the columns are X,Y and Z
i want to compute this Integral
i don't know how to do it in matlab
i searched and didn't find any case like my problem with the integration limits are array values,the variables are also arrays.
and i really don't know how to solve it mathematically
i tried solving ,but the Y,Z and X being arrays with double values is not making things easy
You can do the following if you store X, Y and Z in MATLAB:
fyz = Y*(Z - 25);
% Compute the integral
I = trapz(fyz, X);

zeros of a vector-valued function

Is there any function in MATLAB that can find the zeros of a vector-valued function? The commonly used function fzero is just for scalar functions and also cannot find the zeros of any scalar function such as f(x)=x^2.
Matlab's optimization toolbox has the fsolve method that states it is capable of:
Solves a problem specified by F(x) = 0 for x, where F(x) is a function that returns a vector value. x is a vector or a matrix.
Otherwise, finding zeroes of a generic vector valued function can be done by attempting to minimize the norm of the vectorial output. Lets assume your function F(x) outputs an Nx1 vector. You could attempt to find the zero by doing the following:
y = fminunc(#(x) sum(F(x).^2));
or
y = fminsearch(#(x) sum(F(x).^2));
You would then have to check if the returned y is "sufficiently close" to zero.
One last comment, the fzero function's algorithm determines the existence of roots by checking for sign changes. The [docs] explicitly say that
x = fzero(fun,x0) tries to find a point x where fun(x) = 0. This solution is where fun(x) changes sign. fzero cannot find a root of a function such as x^2.
In fact, in older versions of matlab (R2012b) the fzero's doc had a section with its limitations that said
The fzero command finds a point where the function changes sign. If the function is continuous, this is also a point where the function has a value near zero. If the function is not continuous, fzero may return values that are discontinuous points instead of zeros. For example, fzero(#tan,1) returns 1.5708, a discontinuous point in tan.
Furthermore, the fzero command defines a zero as a point where the function crosses the x-axis. Points where the function touches, but does not cross, the x-axis are not valid zeros. For example, y = x.^2 is a parabola that touches the x-axis at 0. Because the function never crosses the x-axis, however, no zero is found. For functions with no valid zeros, fzero executes until Inf, NaN, or a complex value is detected.
Maybe I misunderstand something in your question, but you can try this solution:
y = #(x) x^2;
fminbnd(y, -100, 100)
ans = -3.5527e-15
And maybe you can try solve:
syms x y
y = #(x) x^2;
solve( y==0, x);
Can't check it right now, I will edit this solution a little bit later.

Matlab fmincon sum of difference squared

I'm a beginner to matlab coding so any help would be appreciated.
I'm trying to minimize difference of summation squared problem SUM((a-b)^2) for 2 variables. I've already coded it up in Excel's Solver like this:
Goal= Sum[{i, 9}, ( Y[i]- (X[i]*m+b) )^2 ]
using nonlinear methods.
where Y and X and arrays, and m and b are the variables we are trying to find by minimizing the sum. How would go do this same thing in Matlab?
thanks.
Here is an example. I've set the bounds by using fmincon.
x=0:10;
y=x*randi(10)-randi(10)+rand(size(x)); % Create data y
f=#(A) sum((y-(A(1)*x+A(2))).^2) % Test function that we wish to minimise
R=fmincon(f,[1 1],[],[],[],[],[0 0],[Inf Inf]) % Run the minimisation R(1)=m, R(2)=b
plot(x,y,x,R(1)*x+R(2)) % Plot the results

calculating bessel function of zero order on matlab

J(x)= 1/π integral cos(xsintheta). limits are from 0 to π.
Plot J(2pid/λ) as a function of d/λ in MATLAB for d/λ ranging between
0 and 2. At what distance of separation (in wavelengths) is the
correlation between the antennas 0.7, 0 ?
I do not understand how to integrate it in matlab, when i define syms theta and use
J_=integral(J,0,pi); there appears an error. secondly, when i integrate it manually, the answer appears 0. Kindly help me with it.
Unless you really need to calculate this manually, you should use Matlab's built-in besselj function to calculate the zeroth order Bessel function of the first kind:
dlam = 0:0.01:2;
x = 2*pi*dlam;
y = besselj(0,x)
figure;
plot(x,y)
This will be faster and more accurate the performing quadrature.
If you wish to determine the to a high degree of accuracy the points at which y is 0.7 or 0, as opposed to reading them from a plot, you can use symbolic math in conjunction with solve and sym/besselj. Assuming that this is what that part of the question is about (I know nothing about antennas), you can use something like:
syms x;
double(solve(besselj(0,x) == 0.7,x))
The integral command does not work on syms, it works on functions. For symbolic integration, the command is int.
I don’t have MATLAB at hand right now to check for typos etc., but something like this should work:
x = 0.1;
integral(#(theta) cos(x.*sin(theta)), 0, pi)/pi
Or even
bessel = #(x) integral(#(theta) cos(x.*sin(theta)), 0, pi)/pi;
bessel(0.1)

Plotting symbolic functions Matlab parameters

I would like to know how to plot functions with parameters in matlab.
For example, the electric potential is described as V(d)=1/(4pi*eps0*d^2)
I would like to plot the electric potential as a symbolic function but to choose which variable to plot as the independent variable.
For example, V(d) or V(eps)... How to tell matlab that d is the variable and eps id the parameter?
Thanks very much
I would go this way:
syms d;
syms eps;
V = 1./(4.*pi.*eps.*d.^2);
You may think of using ezplot capabilities; for instance, you could call
ezplot(subs(V,eps,0.1))
ezplot(subs(V,d,0.1))
In the previous two lines you are saying:
1) plot V depending on d and eps = 0.1;
2) plot V depending on eps and d = 0.1.
I hope this helps.