フィルターのクリア

Function for summing an infinite series using for-end loops

2 ビュー (過去 30 日間)
Chris
Chris 2013 年 10 月 28 日
コメント済み: Youssef Khmou 2013 年 10 月 28 日
The assignment I have is to create a MATLAB function that calculates the value of a function of x as defined as f(x) = (x^2)/37 + (x^3)/3! + (x^4)/4! + (x^5) +.... and test it with a few sets of data for the x value and nth order in which to evaluate. I have created a function, and yet when I try to evaluate it, the output keeps equaling zero for anything I put in there. I'm not completely sure what I'm doing wrong.
Here's the code for the function:
function [x,n]=my_fun(x,n)
x=zeros(size(x));
S=(x^2)/37;
for ii=3:n
S=S+((x^ii)/factorial(ii));
end
end
And here's the code for when I try to use inputs:
x=.25;
n=4;
my_fun(x,n)
Any help would be greatly appreciated. I understand what the program is trying to do, it's just the syntax is a little confusing for me at the moment.

回答 (1 件)

Youssef  Khmou
Youssef Khmou 2013 年 10 月 28 日
Chris,
You can start by differentiating between names of input and output variables , then only S is your output :
function [S]=my_fun(x,n)
S=(x^2)/37;
for ii=3:n
S=S+((x^ii)/factorial(ii));
end
end
  2 件のコメント
Chris
Chris 2013 年 10 月 28 日
Thank you very much, this helped a lot...was making it a lot harder than it actually was.
Youssef  Khmou
Youssef Khmou 2013 年 10 月 28 日
you re welcome, if the problem is solved, you accept the answer .

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by