I'm trying to make use of the integral2 function in MATLAB for an integral:
fun= #(x,y) (sin(x).*exp(-y.^2))/(y.^(2)+1*1+x.^4);
integral2(fun,8,1,2*x,-1);
but when I try to run it, I get an error:
YMIN must be a floating point scalar or a function handle
What is wrong with my ymin value? and how can I resolve it?
The error says that ymin must be a floating point value or a function hanle, but in your case it doesn't seem to be either. converting it to a function handle works. (Also note that you need to use ./ instead of /.)
fun= #(x,y) (sin(x).*exp(-y.^2))./(y.^(2)+1*1+x.^4);
integral2(fun,8,1,#(x)2*x,-1);
Related
In Octave 4.0.2 I have defined a function S as follows:
S = #(x) (Y(k)+((Y(k+1)-Y(k))/h(k)-(2*M(k)+M(k+1))*h(k)/6)*(x-X(k))+M(k)*(k-X(k))^2/2+(M(k+1)-M(k))*(x-X(k))^3/(6*h(k)))
When I call it to evaluate a number in the interval [X(k), X(k+1)] I get the result I expect, but when I try plotting it with the command:
fplot(S, [X(k), X(k+1)]); hold on;
I get the error "error: for A^b, A must be a square matrix. Use .^ for elementwise power."
What is going on?
Alright, I figured out that when passing a function to fplot it must be able to take a vector of inputs and return a vector of outputs.
I'm trying to ezplot this function(f(x)= e^-2t cost t=[-20,20]), and I guess i'm missing the syntax or something.
t=[-20:20]
x= e^-2*t,cos(t)
ezplot(t,x)
but it bringing out an error
you're mixing between x-y plot and function plot, plus you have several syntax errors.
First, t is a 41 elements vector between -21 and 21.
Second, unless you predefined a variable e then e^(-2*t) will give you Undefined function or variable 'e'. error. for exponential function just use exp(-2*t).
Third, assume you fixed the syntax errors such that x = exp(-2*t).*cos(t);, then x is also a 41 elements vector, and therefore you can simply plot it using plot(t,x). ezplot (or fplot in newer versions) is used for plotting functions (rather than vectors). If you want to plot the function in the [-21,21] interval do something like:
f = #(t) exp(-2*t).*cos(t); % this is a function handle
ezplot(f,[-20 20]) % use ezplot with function handle and t interval
x=1;
f=#(x) x^3 - (5/x^2)-4*sin(x)-2;
fzero(f,x)
ans =
1.9227
I am supposed to find the root of the equation, x^3 - (5/x^2)-4*sin(x)-2, and the above code is the solution for it.
I don't understand the general mechanism of this code.
(1) What does # operator do?
I know its something like function handle, but I don't understand what function handle is.
(2) How does it work when it includes x in the parenthesis?
(3) How can there be a function fzero(), when I haven't made a script for fzero()?
(4) why are there two variables inside fzero()?
I don't understand that the variable 'f' does there
(5) Why did it declare x=1 in the beginning?
Please consider that I am pretty much new to MATLAB, and don't know much.
f = #(x) ... is the way to declare an anonymous function in MATLAB, actually not very different than creating a function normally in MATLAB such as function output = f(input) .... It is just the pratical way especially when you are working with mathematical functions.
#(x) defines that x is the variable of which is the same as f(x) in mathematics. fzero() is MATLAB's existing function to calculate the x value for f(x) = 0 which means calculating roots of defined funtion. Giving your x a real value at the beginning does mean the starting point to find the root. It will find the roots greater than 1 in your case. It will be very clear for you when you read existing documentation of MATLAB.
Edit:
If you give an interval such as x = [0 1] instead of x = 1, fzero(f,x) would try to calculate roots of f function in given interval, if there is no roots exist in that interval it woud return a NaN value.
I have a density function f_N which is defined as follows (K_nu(z) is the modified Bessel function):
I want to compute the following integral for each value of N:
The following is the implementation of the above in matlab.
for N=1:100
syms z
f =#(z) (1/(gamma(N)*sqrt(pi))*(z/2).^(N-0.5).*besselk(0.5-N,z));
g = #(z) f(z).*log(f(z));
val=integral(g,0,Inf);
But when I run the above code, it's always returning NaN for varoious values of N with the following warning:
Warning: Infinite or Not-a-Number value encountered
Can someone suggest a simple way to do this or avoid this issue?
I don't think you are doing what you think you are doing. Your declaration of z as a symbol will get overridden by the function handle definition. That means the integral is not a symbolic one, but a numeric one. So what you simply need to do is to drop the function handle notation "#(z)" and do the integral symbolically...
My guess, without thoroughly analysing this, is that one of the points in you integration domain [0,inf] yields a value f (x)=inf, which would destroy a numeric integration technique, but perhaps not a symbolic one.
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.