computation of symbolic terms and error analysis
Afficher commentaires plus anciens
tic
syms x y m %alpha beta b lambda eta phi
phi=sqrt(0.5)
b=10
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
for k=1:3
series(x)=simplify(series(x)+T(k)*(power(x,((k-1)*beta))));
end
series
e1=subs(series,x,1);
format long
accuracy=1e-4;%input('enter the accuracy')
f=e1(x)
g=inline(f)
a=1;%input('enter the ist approximation=')
b=5;%input('enter the 2nd approximation=')
fa=feval(g,a)
fb=feval(g,b)
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
fprintf('the value of c=%f', c);
series1(x)=subs(series,m,c)
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)))
residual_error=double(subs(residualError,x,0.2))
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
le 18 Avr 2026 à 11:08
Déplacé(e) : Torsten
le 18 Avr 2026 à 17:42
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
fh = matlabFunction(f)
Let's check by using subs to substitute values into the symbolic expression and calling the function handle.
check1 = subs(f, x, 1:10)
check2 = fh(1:10)
double(check1) == check2
Catégories
En savoir plus sur Conversion Between Symbolic and Numeric dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






