matlab code which works on 2022a is not working in 2023b
5 views (last 30 days)
Show older comments
syms x y(x)
y1 = x-y^2
%define the function
%define the initial values
x0 = 0
y0 = 1
X = 0.2
h = 0.1
% differentiating successively
y2 = (diff(y1))
y3 = diff(y2)
y4 = diff(y3)
% differentiating order successively
f1=(diff(y1,x,1))
f2=diff(y1,x,2)
f3=diff(y,x,3)
% substitution of values
y10 = subs(y1,{x,y(x)},[x0,y0])
y20 = (subs(y2,{x,y(x),f1},[x0,y0,y10]))
y30 = (subs(y3,{x,y(x),f1,f2},[x0,y0,y10,y20]))
y40 = (subs(y4,{x,y(x),f1,f2,f3},[x0,y0,y10,y20,y30]))
%calculating Taylor series formula
for i=x0+h:h:X
y = y0 + (x-x0)*y10 + (((x-x0)^2)/2)*y20 + (((x-x0)^3)/6)*y30 + (((x-x0)^4)/24)*y40
Y = subs(y,x,i);
fprintf('Value of y at x=%0.1f is %.4f\n',i,Y)
end
4 Comments
Infinite_king
on 29 May 2024
Edited: Infinite_king
on 30 May 2024
I tried to run your code on MATLAB 2022a, but it showed the same exact error as above. The 'subs' function is unable to resolve the symbolic expression with the given substitutions.
What are you trying to achieve? Are you attempting to find the Taylor series expansion of a multivariate function?
Answers (1)
Ganesh
on 29 May 2024
From my understanding you are encountering an error with your given code while executing it in R2023a, but not when executing it in R2021b. However, I find that an error occurs while using both the versions of MATLAB.
The error arises as Y does not evaluate to a valid double number after using the subs() function. Kindly ensure that the implementation of your algorithm is correct. From a simple overview, as "y2","y3" and "y4" is already successively differentiated, substituting "f1","f2" and "f3" seems to cause redundancy.
Later, you need to ensure to convert the variable "Y" to a "double()" value in order to avoid incosistency.
Hope this answer helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!