Rewrite summation formula to MatLab - matlab

I have a following formula:
and I need it to rewrite it to MatLab. The problem is, I'm not very experienced with it, so I'm not sure, if this is the right way to do it.
My code looks like this:
f = #(alpha, beta, gamma, delta)...
alpha*sum((DOF.^(2*beta)) .* log(DOF))...
+ gamma*sum( (DOF.^(beta+delta) .* log(DOF))./nprocs )
DOF and nprocs are vectors of numbers with n elements.
Is it ok or is there some mistake in my code? I'm not sure about the summation of i-th members especially, I'm quite confused by those vector multiplications etc.

Your code looks good.
You do not need .^ - you can just use ^,
assuming beta,delta etc are scalar.
I should mention that the variable DOF and nprocs may be treated as a "closure" - i.e. will be incorporated from the scope in which the function is defined. If you want to avoid this, DOF and nprocs should be included as a parameter.

Related

Matlab function won't plot

Trying to plot a function on matlab, however the graph comes out completely empty
X = linspace(-2,2);
Y = (10*exp(X./10) - 7)/(exp(X.*(33/10)));
plot(X,Y);
You need to use element-wise operation for division:
Y = (10*exp(X./10) - 7)./(exp(X.*(33/10)));
Plotting will work fine then. The problem now is that Y is a one-element array.
I want to add here that, the only operation that should be element-wise operation is the division in the middle. The other operations don't need the '.' as the division of array by scalar as no other meaning. So, it should better be written like this:
Y = (10*exp(X/10) - 7)./(exp(X*(33/10)));
Just a few points to add on to #hesham_EE's excellent answer:
For beginners, it's best to only use * when you are doing matrix multiplication. For arithmetic and/or element-wise operations, stick with .*
It is helpful in debugging to print the output of each row of computation, i.e. omit the semicolon. This allows you to check your syntax. In this case, you would have noticed that Y was not what you had intended it to be.

Matlab SOR Method Implementation

Using an initial approximation of a zero vector and not considering tolerance I have shorten the code to only require 4 arguments. Such that x1 always equals c, and so on by the equation x(k+1)=x(k)T+c.
However the code doesn't seem to produce the correct approximations that you would expect. Does anyone notice where I messed up? Assuming DLU_decomposition(A) returns the correct matrices.
function x = sor2(A,b,omega,kmax)
[D,L,U] = DLU_decomposition(A);
T=inv(D-omega*L)*(((1-omega)*D)+(omega*U));
c= (omega*inv(D-omega*L))*b;
for k=1:kmax,
if(k==1),
x=c;
end
x=T*x+c;
end
norm(A*x-b)
end
Well I can guess all the confusion comes maybe from the multiplications. You need to calculate the matrices elementwise --> use .* instead of the normal *. Would that deliver the correct approximations?

Iterating through an array/vector Matlab

First off, I do have to apologize, because I'm very sure that I'm simply making a simple mistake. I am making my first program in MatLab, and have been reading up on the relevant documentation, but simply still can't seem to solve my problem.
I am trying to implement the equation for information entropy in MatLab (I'm sure it probably already exists, but that's beside the point), but I am having issues with arrayfun as it seems to be calling entropySingle with no arguments.
I have the following functions in appropriately named files
function y = entropySingle(x)
y = x * log2(x);
end
and
function y = entropy(x)
if ~isvector(x)
error('Input must be a vector');
end
x = arrayfun(entropySingle, x);
y = sum(x);
end
and I'm calling entropy([1/3 1/4 1/6 1/8 1/12 1/24]). The error occurs on line 2 of entropySingle, but why is it being called with a null pointer? Thanks in advance,
You need to use element wise multiplication:
y = x .* log2(x);
Not using that small . before the multiplication tell matlab this is about matrix multiplication where it is not.
Also, don't use the name entropy. You are overwriting a built-in matlab function, and this just invites more trouble to your code and life in general.

Is my equation too complex for matlab to integrate?

I have a code that needs to evaluate the arc length equation below:
syms x
a = 10; b = 10; c = 10; d = 10;
fun = 4*a*x^3+3*b*x^2+2*c*x+d
int((1+(fun)^2)^.5)
but all that returns is below:
ans = int(((40*x^3 + 30*x^2 + 20*x + 10)^2 + 1)^(1/2), x)
Why wont matlab evaluate this integral? I added a line under to check if it would evaulate int(x) and it returned the desired result.
Problems involving square roots of functions may be tricky to intgrate. I am not sure whether the integral exists or not, but it if you look up the integral of a second order polynomial you will see that this one is already quite a mess. What you would have, would you expand the function inside the square root, would be a ninth order polynomial. If this integral actually would exist it may be too complex to calculate.
Anyway, if you think about it, would anyone really become any wiser by finding the analytical solution of this? If that is not the case a numerical solution should be sufficient.
EDIT
As thewaywewalk said in the comment, a general rule to calculate these kinds of integrals would be valuable, but to know the primitive function to the particular integral would probably be overkill (if a solution could be found).
Instead define the function as an anonymous function
fun = #(x) sqrt((4*a*x.^3+3*b*x.^2+2*c*x+d).^2+1);
and use integral to evaluate the function between some range, eg
integral(fun,0,100);
for evaluating the function in the closed interval [0,100].

MATLAB | calculating parameters of gamma dist based on mean and probability interval

I have a system of 2 equations in 2 unknowns that I want to solve using MATLAB but don't know exactly how to program. I've been given some information about a gamma distribution (mean of 1.86, 90% interval between 1.61 and 2.11) and ultimately want to get the mean and variance. I know that I could use the normal approximation but I'd rather solve for A and B, the shape and scale parameters of the gamma distribution, and find the mean and variance that way. In pseudo-MATLAB code I would want to solve this:
gamcdf(2.11, A, B) - gamcdf(1.61, A, B) = 0.90;
A*B = 1.86;
How would you go about solving this? I have the symbolic math toolbox if that helps.
The mean is A*B. So can you solve for perhaps A in terms of the mean(mu) and B?
A = mu/B
Of course, this does no good unless you knew B. Or does it?
Look at your first expression. Can you substitute?
gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) = 0.90
Does this get you any closer? Perhaps. There will be no useful symbolic solution available, except in terms of the incomplete gamma function itself. How do you solve a single equation numerically in one unknown in matlab? Use fzero.
Of course, fzero looks for a zero value. But by subtracting 0.90, that is resolved.
Can we define a function that fzero can use? Use a function handle.
>> mu = 1.86;
>> gamfun = #(B) gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) - 0.90;
So try it. Before we do that, I always recommend plotting things.
>> ezplot(gamfun)
Hmm. That plot suggests that it might be difficult to find a zero of your function. If you do try it, you will find that good starting values for fzero are necessary here.
Sorry about my first try. Better starting values for fzero, plus some more plotting does give a gamma distribution that yields the desired shape.
>> B = fzero(gamfun,[.0000001,.1])
B =
0.0124760672290871
>> A = mu/B
A =
149.085442218805
>> ezplot(#(x) gampdf(x,A,B))
In fact this is a very "normal", i.e, Gaussian, looking curve.