Hi Ajshay Verma
1.
A possible approach would be to use the common ODE tools, but, at least in the way I tested them I get the following error messages:
clear all;clc;close all
syms y(t)
[V] = odeToVectorField(diff(y, 2) == 1/t*diff(y) + 2*exp(y)-2*exp(-y))
M = matlabFunction(V,'vars', {'t','Y'})
sol = ode15i(M,[-20 20],[2 3]);
fplot(@(x)deval(sol,x,1), [0 1])
Warning: Failure at t=-1.956270e+01. Unable to meet integration tolerances without
reducing the step size below the smallest value allowed (5.684342e-14) at time t.
> In ode45 (line 308)
Warning: Function behaves unexpectedly on array inputs. To improve performance,
properly vectorize your function to return an output with the same size and shape as
the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 226)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 185)
In fplot>vectorizeFplot (line 185)
In fplot (line 156)
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update:
Attempting to evaluate the solution outside the interval [-2.000000e+01,
-1.956270e+01] where it is defined.
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate.
RCOND = 6.744364e-24.
Warning: Failure at t=-1.956324e+01. Unable to meet integration tolerances without
reducing the step size below the smallest value allowed (6.950260e-14) at time t.
> In ode23s (line 401)
Warning: Function behaves unexpectedly on array inputs. To improve performance,
properly vectorize your function to return an output with the same size and shape
as the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 226)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 185)
In fplot>vectorizeFplot (line 185)
In fplot (line 156)
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update:
Attempting to evaluate the solution outside the interval [-2.000000e+01,
-1.956324e+01] where it is defined.
In ode23t>itsolve (line 919)
In ode23t (line 543)
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
> In ode23t>itsolve (line 919)
In ode23t (line 543)
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
> In ode23t>itsolve (line 919)
In ode23t (line 543)
Warning: Matrix is singular, close to singular or badly scaled. Results may be
inaccurate. RCOND = NaN.
Warning: Failure at t=0.000000e+00. Unable to meet integration tolerances without
reducing the step size below the smallest value allowed (0.000000e+00) at time t.
> In ode23t (line 554)
.
2.
MuPAD gives a solution but, in the way I have used it, only when prompting without the initial conditions y'(0)=0 y'(1)=4 supplied in the question.
.
3.
I also tried a polynomial approximation of y, but as soon as the amount of polynomial terms were increased or the x resolution improved reducing dx, then MATLAB would spend minutes through calculations
close all
clc
clear all
dx=.1;x=[0:dx:1];
N=3;
a=[-5:.1:5];
L=combinator(numel(a),N,'c');
err=zeros(length(L),length(x));
for k=1:1:length(L)
y=zeros(1,length(x));
for s=1:1:N
y=y+a(L(k,s))*x;
end
y=a(L(k,1))*ones(1,length(x))+a(L(k,2))*x+a(L(k,3))*x.^2;
d1y=[y(1) diff(y)];
d2y=[diff(d1y) d1y(end)];
err(k,:)=1./x.*d1y+d2y-2*exp(y)+2*exp(-y);
end
figure;hold all;
for k=1:1:length(L)
plot(err(k,:));
end
grid on;
.
even with 10 terms and really long waiting time, there's unbearable error when x surpasses certain value, polynomials only may not be a good idea to solve this differential equation.
4.
also tried a simplified version of the solution returned by MuPAD, seeking the correct values of the 3 constants I have considered convenient may be a good point to carry on.
close all
clc
clear all
dx=.1;x=[0:dx:1];
N=3;
a=[-5:.1:5];
L=combinator(numel(a),N,'c');
err=zeros(length(L),length(x));
for k=1:1:length(L)
y=zeros(1,length(x));
y=log((a(L(k,1))*x.^2-a(L(k,2))+a(L(k,3))*x)./x.^2);
d1y=[y(1) diff(y)];
d2y=[diff(d1y) d1y(end)];
err(k,:)=d1y+x.*d2y-2*x.*exp(y)+2*x.*exp(-y);
end
figure;hold all;
for k=1:1:length(L)
plot(abs(err(k,:)));
end
grid on;
.
5.
so the function you may want to consider, from MuPAD to MATLAB, is
C1=(2^.5*(C12+2)^.5-2)./x+exp(log(x)*((2*C12+4)^.5-1))./(C13-(2^.5*x.*((2*C12+4).^.5))/(4*(C12+2)^.5))
y=log((x.^2*C1.^2/4-C12/2+x.*C1)/x.^2)
I have not calculated C12 C13, but for instance
C12=1;C13=1;
>>
C1=(2^.5*(C12+2)^.5-2)./x+exp(log(x)*((2*C12+4)^.5-1))./(C13-(2^.5*x.*((2*C12+4).^.5))/(4*(C12+2)^.5))
C1 =
Columns 1 through 8
Inf 4.5323 2.3552 1.7037 1.4549 1.3872 1.4304 1.5595
Columns 9 through 11
1.7679 2.0601 2.4495
y=log((.25*x.^2.*C1.^2-.5*C12+x.*C1)./x.^2)
y =
Columns 1 through 8
NaN -0.7802 -0.4109 -0.1634 0.0407 0.2275 0.4099 0.5964
Columns 9 through 11
0.7930 1.0053 1.2382
.
for a higher resolution x
the shape more or less matches with result in Wolfram DE solver
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG