Sum functions code in matlab

22 ビュー (過去 30 日間)
Somario
Somario 2016 年 3 月 30 日
回答済み: Walter Roberson 2016 年 3 月 30 日
I wrote this matlab codes but I fear something might be wrong.
function F = myfun (x)
format long
syms beta alpha lambda omega n I
n=100;
F1=n-symsum(beta*x(i)^alpha*exp(lambda*x(i)),1,n)-2*symsum(beta*omega*x(i)^alpha*exp(-beta*x(i)^alpha*exp(lambda*x(i))+lambda*x(i))*(1-omega*exp(-beta*x(i)^alpha*exp(lambda*x(i))))^(-1),1,n);
F2=symsum(log(x(i)),1,n)+symsum(1/(alpha+lambda*x(i)),1,n)-symsum(beta*x(i)^alpha*exp(lambda*x(i))*log(x(i)),1,n)-2*symsum(alpha*omega*x(i)^alpha*exp(-beta*x(i)^alpha*exp(lambda*x(i))+lambda*x(i))*log(x(i))*
(1-omega*exp(-beta*x(i)^alpha*exp(lambda*x(i))))^(-1),1,n);
F3=symsum(x(i)/(alpha+lambda*x(i)),1,n)-symsum(beta*x(i)^(alpha+1)*exp(lambda*x(i)),1,n)-symsum(x(i),i,n)--2*symsum(beta*omega*x(i)^(alpha+1)*exp(-beta*x(i)^alpha*exp(lambda*x(i))+lambda*x(i))*(1-omega*exp(-beta*x(i)^alpha*exp(lambda*x(i))))^(-1),1,n);
F4=n/(1-omega)-2*syms(exp(beta*x(i)^alpha*exp(lambda*x(i)))*(1-omega*exp(-beta*x(i)^alpha*exp(lambda*x(i))))^(-1),1,n);
I wish to know if I am correct with the this matlab codes and the look of each function mathematically.
Thank you

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 3 月 30 日
No, that code cannot be correct, as it tries to index with the variable "i" that you have not defined. The default value for the variable named "i" happens to be sqrt(-1) which is something you cannot index by.
You should be using
syms i
and listing i in your symsum(), in the form
symsum(EXPRESSION, i, 1, n)
However! A recent posting explored that you cannot index by a symbolic variable. So you should not be using symsum(). You should be using regular sum()
For example,
symsum(beta*x(i)^alpha*exp(lambda*x(i)),1,n)
but
sum(beta .* x.^alpha .* exp(lambda .* x))

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by