computation of symbolic terms and error analysis

tic
syms x y m %alpha beta b lambda eta phi
phi=sqrt(0.5)
phi = 0.7071
b=10
b = 10
eta=1
eta = 1
alpha=2;
beta=1;
T=zeros(1,2,'sym');
G=zeros(1,2,'sym');
A=zeros(1,'sym');
B=zeros(1,'sym');
C=zeros(1,'sym');
D=zeros(1,'sym');
E=zeros(1,'sym');
F=zeros(1,'sym');
G_val=zeros(1,'sym');
series(x)=sym(zeros(1,1));
series1(x)=sym(zeros(1,1));
% Transform Initial condition
T(1)=m;
T(2)=0;
% Initial transform value of exp function
for i=1:4
G(i)=y;
if i==1
E(1)=exp((eta*T(1))/(eta+T(1)));
else
A(1)=0;
for j=1:i-1
E(i)=A(1)+(j/(i-1))*G(j)*E(i-j);
end
B(1)=0;
for j=1:i
B(1)=B(1)+T(j)*G(i-j+1);
end
eq1=G(i)-T(i)+(1/eta)*B(1);
G_val=simplify(solve(eq1,y));
G(i)=simplify(G_val);
E_val=subs(E(i),y,G_val);
E(i)=E_val
end
C(1)=0
for r=1:i
C(1)=C(1)+T(r)*E(i-r+1);
end
T(i + alpha*beta) = (phi^2*C(1)-b*phi^2*E(i))*(gamma(1+(sym(i-1)/beta))/gamma(1+alpha+(sym(i-1)/beta)));
%T(i+2)=simplify((1/(i*(i+1)))*(phi^2*C(1)-b*phi^2*E(i)));
end
C = 
0
E = 
C = 
0
E = 
C = 
0
E = 
C = 
0
for k=1:3
series(x)=simplify(series(x)+T(k)*(power(x,((k-1)*beta))));
end
series
series(x) = 
e1=subs(series,x,1);
format long
accuracy=1e-4;%input('enter the accuracy')
f=e1(x)
f = 
g=inline(f)
g = Inline function: g(m) = m-exp(m./(m+1.0)).*(5.0./2.0)+(m.*exp(m./(m+1.0)))./4.0
a=1;%input('enter the ist approximation=')
b=5;%input('enter the 2nd approximation=')
fa=feval(g,a)
fa =
-2.709622859075289
fb=feval(g,b)
fb =
2.123780136383969
while fa*fb>0
a=input('enter the ist approximation=')
b=input('enter the 2nd approximation=')
fa=feval(g,a)
fb=feval(g,b)
end
for i=1:50
c=(a+b)/2;
fc=feval(g,c);
disp([i a fa b fb c fc abs(b-a)])
if fc==accuracy
fprintf('the root of the equation is %f',c)
break;
elseif abs(b-a)<=accuracy
fprintf('the root of the equation is %f',c)
break;
elseif fa*fc<=0
b=c;
fb=fc;
else
a=c;
fa=fc;
end
end
1.000000000000000 1.000000000000000 -2.709622859075289 5.000000000000000 2.123780136383969 3.000000000000000 -0.704750029072181 4.000000000000000 2.000000000000000 3.000000000000000 -0.704750029072181 5.000000000000000 2.123780136383969 4.000000000000000 0.661688607261298 2.000000000000000 3.000000000000000 3.000000000000000 -0.704750029072181 4.000000000000000 0.661688607261298 3.500000000000000 -0.037023639038904 1.000000000000000 4.000000000000000 3.500000000000000 -0.037023639038904 4.000000000000000 0.661688607261298 3.750000000000000 0.309004610855695 0.500000000000000 5.000000000000000 3.500000000000000 -0.037023639038904 3.750000000000000 0.309004610855695 3.625000000000000 0.135098495155879 0.250000000000000 6.000000000000000 3.500000000000000 -0.037023639038904 3.625000000000000 0.135098495155879 3.562500000000000 0.048806291793267 0.125000000000000 7.000000000000000 3.500000000000000 -0.037023639038904 3.562500000000000 0.048806291793267 3.531250000000000 0.005832481645888 0.062500000000000 8.000000000000000 3.500000000000000 -0.037023639038904 3.531250000000000 0.005832481645888 3.515625000000000 -0.015610425294633 0.031250000000000 9.000000000000000 3.515625000000000 -0.015610425294633 3.531250000000000 0.005832481645888 3.523437500000000 -0.004892666418066 0.015625000000000 10.000000000000000 3.523437500000000 -0.004892666418066 3.531250000000000 0.005832481645888 3.527343750000000 0.000468986086110 0.007812500000000 11.000000000000000 3.523437500000000 -0.004892666418066 3.527343750000000 0.000468986086110 3.525390625000000 -0.002212070812502 0.003906250000000 12.000000000000000 3.525390625000000 -0.002212070812502 3.527343750000000 0.000468986086110 3.526367187500000 -0.000871599991724 0.001953125000000 13.000000000000000 3.526367187500000 -0.000871599991724 3.527343750000000 0.000468986086110 3.526855468750000 -0.000201321355804 0.000976562500000 14.000000000000000 3.526855468750000 -0.000201321355804 3.527343750000000 0.000468986086110 3.527099609375000 0.000133828764920 0.000488281250000 15.000000000000000 3.526855468750000 -0.000201321355804 3.527099609375000 0.000133828764920 3.526977539062500 -0.000033747195564 0.000244140625000 16.000000000000000 3.526977539062500 -0.000033747195564 3.527099609375000 0.000133828764920 3.527038574218750 0.000050040559656 0.000122070312500 17.000000000000000 3.526977539062500 -0.000033747195564 3.527038574218750 0.000050040559656 3.527008056640625 0.000008146625789 0.000061035156250
the root of the equation is 3.527008
fprintf('the value of c=%f', c);
the value of c=3.527008
series1(x)=subs(series,m,c)
series1(x) = 
residualError=sym(zeros(1,1));
residual=sym(zeros(1,1));
residualError(x) = abs(diff(series1,x,2)+ B*phi^2*(1-(series1)/B)*exp((eta*series1)/(eta+series1)))
residualError(x) = 
residual_error=double(subs(residualError,x,0.2))
Error using mupadengine/evalin2double
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Error in mupadengine/feval2double

Error in sym/double (line 756)
X = feval2double(symengine, "symobj::doubleDirect", S);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
var =double(residual_error);
x=0:0.1:1
error=zeros(1)
row=0;
for i=1:length(x)
row=row+1;
error(row)=(residualErrorx(i))
max_error=max(error)
end
fprintf('The residual error is %f\n', max_error);
%-----------------------------------------------------------------
the error appear as Error using mupadengine/evalin2double
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
Error in mupadengine/feval2double
Error in sym/double (line 756)
X = feval2double(symengine, "symobj::doubleDirect", S);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Related documentation
now i substutite the value of m using sysntax series1(x)=subs(series,m,c) but when i later on call series 1 in line residualError(x) = abs(diff(series1,x,2)+ B*phi^2*(1-(series1)/B)*exp((eta*series1)/(eta+series1))) the final expression is having m and y . That is symbolic and craet an error

1 commentaire

yogeshwari patel
yogeshwari patel le 18 Avr 2026 à 11:08
Déplacé(e) : Torsten le 18 Avr 2026 à 17:42
I find the solution . Thanks

Connectez-vous pour commenter.

Réponses (1)

You indicated you've already found the solution (the expression you're trying to convert to double still has symbolic variables m and y inside it) but I wanted to call out one change you should make. It is with this line of code (left in a block comment so that I can run later code in this answer):
%{
g=inline(f)
%}
Don't use the inline function. It is not recommended and has been "not recommended" for years if not decades. To convert a symbolic expression into something you can evaluate directly, use matlabFunction instead. Or use subs to substitute values into a symbolic expression.
syms x
f = x^2+2*x+3
f = 
fh = matlabFunction(f)
fh = function_handle with value:
@(x)x.*2.0+x.^2+3.0
Let's check by using subs to substitute values into the symbolic expression and calling the function handle.
check1 = subs(f, x, 1:10)
check1 = 
check2 = fh(1:10)
check2 = 1×10
6 11 18 27 38 51 66 83 102 123
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
double(check1) == check2
ans = 1×10 logical array
1 1 1 1 1 1 1 1 1 1

Question posée :

le 18 Avr 2026 à 10:48

Réponse apportée :

le 18 Avr 2026 à 18:23

Community Treasure Hunt

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

Start Hunting!

Translated by