I am struggling to store a function without it being evaluated so the function can be differentiated later.
20 次查看(过去 30 天)
显示 更早的评论
This is my code:
function res = tayexp(x,a,n)
if x <= 0
error ("x is not within the domain")
elseif isinteger(n) == 0 && sign(n) <= 0
error ("n must be a positive integer")
elseif sign(a) <= 0
error ("a cannot be less than or equal to zero")
else
fd=[];
f2 = [];
startfunc(t) = 1./t + log(t)
for i = (0:1:n-1)
newfunc = diff(startfunc(), i+1);
fd = [fd, subs(newfunc,t,x)];
f2 = [f2, (fd(i+1)*(a)*((x-a)^i)) / factorial(i)];
end
res = sum(f2, "all");
end
%goal: make an array of each derivative to the nth degree for 1/x + log(x)
%from there, evaluate each element at x to make an array of numbers
%then plug each value into the taylor series
My issue is that startfunc insists on being evaluated, leading to an error with t which is just an independent variable without a given value.
I have tried nested functions, i have tried making t an empty array, i have tried the @(t) syntax, and none of it seems to work. Any help would be greatly appreciated.
0 个评论
回答(1 个)
Steven Lord
2024-10-23,4:36
The correct syntax to make that into an anonymous function is:
startfunc = @(t) 1./t + log(t)
Then evaluating it works like:
y = startfunc(1:5)
2 个评论
Walter Roberson
2024-10-23,4:43
newfunc = diff(startfunc(), i+1);
will need to be changed to
syms t
newfunc = diff(startfunc(t), i+1);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Events 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!