Can someone please provide me with the code for this plot

2 ビュー (過去 30 日間)
Akshay Verma
Akshay Verma 2017 年 9 月 3 日
コメント済み: John BG 2017 年 9 月 5 日
(1/x)(d/dx((x(dy/dx)))) = 2*(exp(y) - exp(-y)) : x varies from 0 to 1 [Dy(0)=0 , Dy(1)=4]
Plot y as a function of x

採用された回答

John BG
John BG 2017 年 9 月 4 日
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])
%ode54 ode113 ode23tb return the following
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.
% In ode23s (line 379)
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.
% ode23t returns NaNs
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; % amount terms
a=[-5:.1:5]; % accuracy terms
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; % amount terms
a=[-5:.1:5]; % accuracy terms
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
dx=.01;x=[0:dx:1];
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
  4 件のコメント
John BG
John BG 2017 年 9 月 5 日
編集済み: John BG 2017 年 9 月 5 日
Hi Akshay
combinator is a wonderful function written by Loginatorist, available from
I installed an earlier version, then an upgrade came up, but have not yet installed the new version, perhaps there's a variation in how to use it.
To find out, check the initial comments, just below the function declaration buy putting the combinator.m in your current MATLAB folder, and then
type combinator
scroll up to the very 1st function, Loginatorist provides a detailed explanation how to use this function.
Regards
John BG
John BG 2017 年 9 月 5 日
please note that points 3 and 4 are only error checks, and the x step is wider than you probably need, just to save simulation time.
The function you need to start with is MuPAD result.
Just in case, MuPAD is the German Symbolic simulator developed in Germany, a product that initially was direct competition against other Mathematical simulation software like Maple, Wolfram and MATLAB.
It's the same story we keep seeing from European Engineering companies, university start-ups that keep copying American products, like ProEngineer and Katia against Autodesk, Linux against Windows, GNSS against GPS, Airbus against Boeing, it's literal cloning in the strategic satellite sections, so on so forth.
The people at Mathworks did the smart move to purchase MuPAD an include in the current Symbolic Toolbox, but some MuPAD functionality can be used without having purchased the Symbolic Toolbox.
Wolfram is a direct competitor to MATLAB, no Simulink in Wolfram's portfolio, but their differential equations solver comes handy, something not included in the base MATLAB product.
Perhaps you would like to have a look at MATLAB's PDE toolbox

サインインしてコメントする。

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2017 年 9 月 3 日
When you solved the equation, it becomes y=(x^3/3)*Exp(-1)
%
x=0:0.1:1;
t=exp(-1);
y=((x.^3)/3)*t;
plot(x,y);
%
  2 件のコメント
Akshay Verma
Akshay Verma 2017 年 9 月 3 日
How did you solve the equation
Akshay Verma
Akshay Verma 2017 年 9 月 3 日
And even the plot is not correct

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Solvers についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by