How to obtain array output from nested loops in user defined function
显示 更早的评论
Hi, I am trying to make a user defined function that will find e^x using the Taylor series. What I have is a While loop nested in a For loop in the function (see code). The function works great for single inputs of 'x', giving a single correct y value. However, when I try to enter a row vector for x, it only gives the final answer of the final term in the vector. When I make it show every value S (the final e^x value) it will show each output for each input, but as a separate answer and is useless because I will need them outputted together as a single matrix. Thanks for any help!
function [ y ] = mae_exp(x, E )
%x is the power you want to take 'e' to, E is the error you wish to have
f=1:1:length(x);
for X=x(1,f)
D = 100; n = 0; A=1; S=A;
while D>E
n = n+1; %stepping to next term
A = X.^(n)./factorial(n); %finding answer of n term
D = abs((S-(S-A))./(S-A)); %calculating error
S = A + S; %adding the all current terms together
end
y=S
end
end
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!