Evaluating a complex integral in MATLAB - matlab

Hello I'm trying to integrate the following function in MATLAB
And this is my attempt at evaluating it at a given (x,y)
fun = #(t,x,y) exp(1i.*(t.^4+x.*t.^2+y.*t));
P = #(x,y) integral(#(t)fun(t,x,y),-Inf,Inf);
P(1,1)
According to WolframAlpha the answer is 1.20759 + 0.601534 i for P(1,1)
but MATLAB returns -6.459688464052636e+07 - 8.821747942103466e+07i
I am wondering how to enter an integral like this correctly.
I have now also tried evaluating this symbolically and using a taylor series to approximate but still no luck.
syms x y t
x=1
y=1
f = exp(1i*(t^4+x*t^2+y*t));
fApprox = taylor(f, t, 'ExpansionPoint', 0, 'Order', 10)
sol=int(fApprox,t,[-inf inf])
Any additional suggestions
Many thanks in advance.

Related

MATLAB: How to solve high dimensional nonlinear ODE?

I have an ODE:
x' = -x + f(x)
Looks simple enough, but x is 100 dimensional i.e.
x = [x1, ... , x100]
Furthermore,
fi(x) = ln(xi)/(ln(x1)+...+ln(x100))
where i is between 1 and 100 and f(x) = [f1(x), ... , f100(x)]
On MATLAB's website, it says I should first create a function as:
But how can I do this? I have 100 variables, and all my variables are coupled through that highly nonlinear function.
Any help is greatly appreciated!
The function can leverage the vectorization capabilities of MATLAB since, from the ode45 documentation, the "function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt". So your odefun can be expressed simply as
function dxdt = odefun(~,x)
logX = log(x);
dxdt = -x + logX/sum(logX);
end
and let ode45, or another appropriate integrator, handle the rest.

Drawing a plot and calculating area in matlab

I need to draw the curve of x^3+y^3-3*axy=0 and calculate its area. I'm new to matlab so I don't know much. Any help would be appreciated.
Here's what I came up with already`
function makeres(a)
syms x y;
k=y.^3+x.^3-3.*x.*a.*y==0;
ezplot(k)
fun=#(x)(k);
integral(integral(k,0,5),0,5)
But I get an error on integral.
Try this:
syms x y;
k=y.^3+x.^3-3.*x.*a.*y==0;
ezplot(k)
fun=#(x,y)y.^3+x.^3-3.*x.*a.*y;
g = integral2(fun,0,5,0,5);
Your erros where in the last 2 lines:
a - you were defining k (which is an equation) as the function to be integrated
b - then you integrated k, not fun
c - it is better use integral2 for double integral
I'm don't know what a should be, it works for me when a is a number, an the code as a single script (not a defined function).

How to perform indefinite integration of this function in MATLAB?

I need to perform the following operations as shown in the image. I need to calculate the value of function H for different inputs(x) using MATLAB.
I am giving the following command from Symbolic Math Toolbox
syms y t x;
f1=(1-exp(-y))/y;
f2=-t+3*int(f1,[0,t]);
f3=exp(f2);
H=int(f3,[0,x]);
but the value of 2nd integral i.e. integral in the function H can't be calculated and my output is of the form of
H =
int(exp(3*eulergamma - t - 3*ei(-t) + 3*log(t)), t, 0, x)
If any of you guys know how to evaluate this or have a different idea about this, please share it with me.
Directly Finding the Numerical Solution using integral:
Since you want to calculate H for different values of x, so instead of analytical solution, you can go for numerical solution.
Code:
syms y t;
f1=(1-exp(-y))/y; f2=-t+3*int(f1,[0,t]); f3=exp(f2);
H=integral(matlabFunction(f3),0,100) % Result of integration when x=100
Output:
H =
37.9044
Finding the Approximate Analytical Solution using Monte-Carlo Integration:
It probably is an "Elliptic Integral" and cannot be expressed in terms of elementary functions. However, you can find an approximate analytical solution using "Monte-Carlo Integration" according to which:
where f(c) = 1/n Σ f(xᵢ)
Code:
syms x y t;
f1=(1-exp(-y))/y; f2=-t+3*int(f1,[0,t]); f3=exp(f2);
f3a= matlabFunction(f3); % Converting to function handle
n = 1000;
t = x*rand(n,1); % Generating random numbers within the limits (0,x)
MCint(x) = x * mean(f3a(t)); % Integration
H= double(MCint(100)) % Result of integration when x=100
Output:
H =
35.2900
% Output will be different each time you execute it since it is based
% on generation of random numbers
Drawbacks of this approach:
Solution is not exact but approximated.
Greater the value of n, better the result and slower the code execution speed.
Read the documentation of matlabFunction, integral, Random Numbers Within a Specific Range, mean and double for further understanding of the code(s).

Fitting data in least square sense to nonlinear equation

I need help fitting data in a least square sense to a nonlinear function. Given data, how do I proceed when I have following equation?
f(x) = 20 + ax + b*e^(c*2x)
So I want to find a,b and c. If it was products, I would linearize the function by takin the natural logaritm all over the function but I can not seem to do that in this case.
Thanks
You can use the nlinfit tool, which doesn't require the Curve Fitting Toolbox (I don't think...)
Something like
f = #(b,x)(20 + b(1)*x + b(2)*exp(b(3)*2*x));
beta0 = [1, 1, 1];
beta = nlinfit(x, Y, f, beta0);
When MATLAB solves this least-squares problem, it passes the coefficients into the anonymous function f in the vector b. nlinfit returns the final values of these coefficients in the beta vector. beta0 is an initial guess of the values of b(1), b(2), and b(3). x and Y are the vectors with the data that you want to fit.
Alternatively, you can define the function in its own file, if it is a little more complicated. For this case, you would have something like (in the file my_function.m)
function y = my_function(b,x)
y = 20 + b(1)*x + b(2)*exp(b(3)*2*x);
end
and the rest of the code would look like
beta0 = [1, 1, 1];
beta = nlinfit(x, Y, #my_function, beta0);
See also: Using nlinfit in Matlab?
You can try the cftool which is an interactive tool for fitting data. The second part I don't quite understand. It may help if you describe it in more detail.

fzero with a matrix and solution of previous fzero as an input

This is an evolution of the question asked by Immo here
We have a function f(x,y) and we are trying to find the value of x (x*) for a function f(x,y) that produces the function value 0 for a given Y, where Y is a large vector of values.
x* = arrayfun(#(i) fzero(#(x) minme(y(i),x),1),1:numel(y))
What if now I would like to use the x* solution of the arrayfun to find another fzero of the second equation, lets say minme2, that also depends on the same large vector of values Y.
minme2 = #(y,x*, x) x - Y + x* + x* ./ (const1 * (const2 - x*))
Moreover I would like my solutions to dynamically depend on the interval I am selecting for the fzero solution, rather than the initial guess. Instead of 1 I would like my solutions to be found in between:
x0 = [0, min(const1, x*)];
I would appreciate answers how can I solve the minme2 as currently I am facing errors.