Solve equation Matlab - matlab

Hy can anyone can tell me how i can solve this equation
2xJ1(x) − J0(x) = 0
where J1 and J0 are the Bessel function
in Matlab without using the fsolve command

You can use fzero to find the zeros if and only if the curve crosses the x-axis. So
fzero(#(x)(2*x*besselj(1,x)-besselj(0,x)),x0)
will give you the zero close to x0.
See this answer of mine for an explanation of when and why fzero fails (although it won't for a Bessel function, it's good to be aware) and the tradeoffs with fsolve.

Related

Problem on numerical solution of y'=y^2+1

I would like to use numerical approach to calculate the differential with singularities.
For instance, y'=y^2+1 with y(0)=0. The analytical solution is not hard to find, y=tan(x).
The problem arises when I apply the numeric method, see the code in Matlab
tspan = [0 6];
y0 = [0; 0];
ode = #(t, y) y.^2+1;
[t, y] = ode45(ode, tspan, y0);
plot(t, y(:,1))
axis([0 6 -20 20])
which gives the plot below, i.e., it missed the part of solution after the first singularity.
My question: how to find the full numeric solution of a differential equation with singularities?
Thanks in advance!
This is impossible in general. An ODE solution ends where it diverges to infinity (or leaves the domain of the ODE function in any other way).
What you can do is apply domain knowledge. This equation is a Riccati DE. If one knows one solution, one can transform it into a Bernoulli DE that has a solution formula. There is a second transform without knowledge of a particular solution, it works always but is not always helpful. Set y=p/q and select their relationship so that a system of linear equations results. Here p'q-q'p=p^2+q^2 resolves nicely to p'=q, q'=-p, or as second order DE u''+u=0, the harmonic oscillator, for u=q or u=p. This system now has no singularities and the poles of the original solution y are the roots of the denominator q.

Trouble plotting a solution curve with dsolve

I'm trying to do my assignments which is plotting a direction fields and a solution curve that passes a given point and having a trouble doing this particular differential equation: y'=1-x*y, y(0)=0.
My code is :
syms x y;
y1=dsolve('Dy=1-x*y','y(0)=0','x');
y1=expand(y1);
ezplot(y1,[-10 10 -10 10]);
And it got some error about the input i believe, it says:
Error using inlineeval and error in expression, input must be real and
full and so on...
I have had success with other differential equations but this one is still miserable.
I'm almost sure this is a comment, but since I still don't have enough reputation to do so I'm answering here :P
Apparently the solution y1 for your equation is something like
y1 = -(2^(1/2)*pi^(1/2)*exp(-x^2/2)*erf((2^(1/2)*x*1i)/2)*1i)/2
The erf function needs a real input, but in this case you have a 1i complex term which is causing the problem.

Solve maximization problem of quadratic equation in MATLAB

How do I solve a quadratic Maximization problem in MATLAB? It seems MATLAB only supports minimization problems, so is there a mathematical concept I can use?
simply multiply by (-1) before and after using the minimization function
Using quadprog function in MATLAB.
This function solves Quadratic Programming problems in MATLAB.
Of course if you want the maxima instead of the minima, you can multiply the cost function by -1.
Good Luck.
The above answer #Drazick seems not right.
quadprog() in matlab requires H to be positive definite. If we simply multiply (-1), -H is a negative definite matrix, which violates the requirement.
Another optimization function called fmincon( ) may help.

How to solve complex system of equations using matlab?

I have to analyze 802.11 saturation throughput using matlab, and here is my problem. I'm trying to solve parametric equations below (parameters are m,W,a) using solve function and i get
Warning: Explicit solution could not be found
How could I solve above equations using matlab?
I guess you were trying to find an analytical solution for tau and p using symbolic math. Unless you're really lucky with your parameters (e.g. m=1), there won't be an analytical solution.
If you're interested in numerical values for tau and p, I suggest you manually substitue p in the first equation, and then solve an equation of the form tau-bigFraction=0 using, e.g. fzero.
Here's how you'd use fzero to solve a simple equation kx=exp(-x), with k being a parameter.
k = 5; %# set a value for k
x = fzero(#(x)k*x-exp(-x),0); %# initial guess: x=0

Plotting and finding roots of bessel functions

I am trying to plot roots of a function that is composed of multiple bessel functions being added and multiplied in Matlab. The equation is Jm(omega)*Ik(omega)+Im(omega)*Jk(omega) where Jm is the bessel function of the first kind of order m (besselj). Im is the modified bessel function of the first kind of order m (besseli). For each mode m=o,1,2,...and n=1,2,3... The frequency omega(mn) is the corresponding root of the listed equation. m=0,1,2 n-1,2,3,4. I need to solve the equation for the 12 roots. I am new to Matlab and this is a little out of my league. So far I have this code but I wasn't sure if I needed the variable omega in the script or not. I have also looked at other people's questions on the suject but didn't see any quite like this. The plots I have seen look nothing like mine which tells me I am probably wrong. Thanks for any help.
m=(0:2); k=(1:3); n=(1:4);
Jm=besselj(m,n');
Ik=besseli(k,n');
Jk=besselj(k,n');
Im=besseli(m,n');
g=Jm.*Ik+Im.*Jk
plot(g)
Plotting
besselj and besseli take what you call omega as their second parameter, so to plot your function you should try something like
m=0; k=1; omega=0:0.02:10;
Jm=besselj(m,omega);
Ik=besseli(k,omega);
Jk=besselj(k,omega);
Im=besseli(m,omega);
g=Jm.*Ik+Im.*Jk;
plot(omega,g);
hold all;
plot(omega,0,'k');
axis([min(omega) max(omega) -100 100]);
This shows you that for m=1, k=1 the first zeros are around 3.2, 6.3 and 9.4:
Finding the roots numerically
You could implement Halley's method for your function g, similar to how the roots of besselj are determined in the MatlabCentral file linked by Cheery.