Function with Non Constant Variable
11 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm wondering if there's a way to run this code while keeping x a variable (Instead of inputting a number for the required input 'x', have a variable so I can create a graph of results). Currently I just recieve output 'Unrecognized Function or variable 'x', even when putting sym x in the code.
Thanks for any help!
4 个评论
per isakson
2020-10-27
x must have a value, e.g.
%%
x = 17;
S(3,1,x,2)
Adam Danz
2020-10-27
" I want the x input to work as a variable"
This part is still unclear, hence the variety of answers. Do you mean you want to pass a variable to the 3rd input (see per isakson's or Stephen Cobeldick's answer) or do you mean that the 3rd variable should be a symbolic variable (see Walter Roberson's answers).
回答(3 个)
Walter Roberson
2020-10-27
syms x
output = S(3, 1, x, 2);
disp(output)
output = S(3, 1, 4, 2);
disp(output)
function SF = S(F, a, x, n)
if isa(a, 'sym') || isa(x, 'sym')
SF = piecewise(x >= a, F*(x-a)^n, 0);
elseif x >= a
SF = F.*(x-a).^n;
else
SF = zeros(size(x));
end
end
1 个评论
Walter Roberson
2020-10-27
syms x
output = S(3, 1, x, 2);
disp(output)
output = S(3, 1, 4, 2);
disp(output)
function SF = S(F, a, x, n)
if isa(a, 'sym') || isa(x, 'sym')
SF = piecewise(x >= a, F*(x-a)^n, 0);
elseif x >= a
SF = F.*(x-a).^n;
else
SF = zeros(size(x));
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!