Is it possible to do a summation series in Octave?
In matlab there is symsum function for it, however I didn't found anything similar for octave.
For example, I want to find the following sum
Addendum:
Whether it's possible to sum something like this
f = #(x) nchoosek(5,x)*0.1.^x*0.9.^(5-x)
sum(f([0:5]))
Failed with error
error: called from:
error: /usr/share/octave/3.6.4/m/help/print_usage.m at line 87, column 5
error: /usr/share/octave/3.6.4/m/specfun/nchoosek.m at line 95, column 5
error: at line -1, column -1
error: evaluating argument list element number 1
If you don't need an analitical solution, then you don't need symsum. For example if you want to calculate
the you can simply use sum
sum([1:5])
Here is another example:
f = #(x) exp(-x)
sum(f([1:5]))
And another one with factorial function:
g = #(n) 1 ./ factorial(n)
sum(g([0:5]))
the same, but without anonymous function:
sum(1 ./ factorial([0:5]))
Update
As for your last example, nchoosek allows only scalar arguments. So, you'll need additional arrayfun call:
f = #(x) nchoosek(5,x)*0.1.^x*0.9.^(5-x)
sum(arrayfun(f,[0:5]))
Related
Basically, I have this function in matlab:
function yy = homo2nd(tt,polycc,y0,v0)
rr = roots(polycc);
cc = [1 1; rr(1) rr(2)\[y0; v0]];
yy = cc(1)*exp(rr(1)*tt) + cc(2) * exp(rr(2)*tt);
yy = round(yy,8);
end
I understand that roots takes the roots of an array signifying the coefficients of the left side of the equation. I feed roots and argument like [1 1 1] and it spits out the eigenvalues of that function.
From there I am lost.
I define tt in the command shell like this:
tt = linspace(0,2*pi,100).
I call homo2nd and feed it arguments (tt,[1 1 1], 1, 1)
When I try to feed roots the argument [1 1 1] like I said I get this error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
I just need help interpreting the code. Why are the arrays horizontally mismatched?
The error is occuring on this line
cc = [1 1; rr(1) rr(2)\[y0; v0]];
So let's look at this, the 1 1 is fine, it's saying there's a row vector [1,1].
The next part is where the error occurs, the ; moves us to a second row. The first element of that row is rr(1), this is fine.
However, the second element rr(2)\[y0; v0] is using the mldivide command and the result of this isn't a single element, hence the error.
I have a vector v and would like to define a function x using following formular:
Now I try to define this in Matlab, but for some reason I can't access an array within a symsum.
v = [1 2 3 4];
syms k
x = #(t) symsum(v(k) * exp(2*pi*i*k*t/size(v)), k, 1, size(x_n));
When I try to evaluate this Function x(4) I get an exception:
Error using sym/subsindex (line 796)
Invalid indexing or function definition. When defining a function, ensure that the arguments are
symbolic variables and the body of the function is a SYM expression. When indexing, the input
must be numeric, logical, or ':'.
Do you know, why? Do you have a solution or workaround?
I am having trouble using fminsearch: getting the error that there were not enough input arguments for my function.
f = #(x1,x2,x3) x1.^2 + 3.*x2.^2 + 4.*x3.^2 - 2.*x1.*x2 + 5.*x1 + 3.*x2 + 2.*x3;
[x, val] = fminsearch(f,0)
Is there something wrong with my function? I keep getting errors anytime I want to use it as an input function with any other command.
I am having trouble using fminsearch [...]
Stop right there and think some more about the function you're trying to minimize.
Numerical optimization (which is what fminsearch does) is unnecessary, here. Your function is a quadratic function of vector x; in other words, its value at x can be expressed as
x^T A x + b^T x
where matrix A and vector b are defined as follows (using MATLAB notation):
A = [ 1 -1 0;
-1 3 0;
0 0 4]
and
b = [5 3 2].'
Because A is positive definite, your function has one and only one minimum, which can be computed in MATLAB with
x_sol = -0.5 * A \ b;
Now, if you're curious about the cause of the error you ran into, have a look at fuesika's answer; but do without fminsearch whenever you can.
It is exactly what Matlab is telling you: your function expects three arguments. You are passing only one.
Instead of
[x, val] = fminsearch(f,0)
you should call it like
[x, val] = fminsearch(f,[0,0,0])
since you define the function f to accept a three dimensional vector as input only.
You can read more about the specification of fminsearch in the online documentation at http://mathworks.com/help/matlab/ref/fminsearch.html:
x = fminsearch(fun,x0) starts at the point x0 and returns a value x
that is a local minimizer of the function described in fun. x0 can be
a scalar, vector, or matrix. fun is a function_handle.
I need help figuring out how to code the following problem. Any help would be greatly appreciated!
Create a function that will take a vector/array input for x (1 by n) and a scalar input for a, and produce the output defined by the following equation:
y(x,a)=((xsin(ax-2))/(sqrt(1+(ax)^2)
-π ≤ x ≤ π
a={.5 1 1.5 2}
The equation must be vectorized in terms of x and the output from the function is the array y which has the same dimension as the array x.
Write a script that calls this function to compute y(x,a) for the range of x defined above and each value of the parameter a. Results should be stored in a solution matrix using a different row of the solution matrix for each value of a.
So far for my function I have:
function [y] = part1(a,x)
y=((x*sin(a*x-2))/(sqrt(1+(a*x).^2)));
end
I'm not sure how to output this into the solution matrix
For my script I have:
%%
clear,clc
a={0.5 1 1.5 2};
x=-pi:0.1:pi;
for
part1(x,a)
end
I'm getting the following errors when I run this now:
Undefined function 'mtimes' for input arguments of type 'cell'.
Error in part1 (line 4)
y=((x*sin(a*x-2))/(sqrt(1+(a*x).^2)));
Error in labtest2 (line 8)
y(i,:)=part1(x,a(i));
EDIT
I've made some changes and am still getting some errors that I cannot resolve.
Here is my full code for function followed by full code for script:
Function
function [y] = part1(x,a)
nx=numel(x);
na=numel(a);
y=((x.*sin(a.*x-2))./(sqrt(1+(a.*x).^2)));
size(y)=[nx na]
end
Script
%%
clear,clc
a={0.5 1 1.5 2};
x=-pi:0.1:pi;
for i = 1:length(a)
y(i,:)=part1(x,a(i));
end
Errors
Undefined function 'times' for input arguments of type 'cell'.
Error in part1 (line 6)
y=((x.*sin(a.*x-2))./(sqrt(1+(a.*x).^2)));
Error in labtest2 (line 8)
y(i,:)=part1(x,a(i));
The reason you're getting Undefined function 'times' for input arguments of type 'cell' is because your variable a is a cell array. You need to change your assignment of a from
a={0.5 1 1.5 2};
to
a=[0.5 1 1.5 2];
which will make it just a normal array. Alternatively, you need to reference it with cell array notation: a{i} instead of a(i).
You're almost there. Note that you've written
function [y] = part1(a,x)
but you call it in your script as
part1(x,a)
so you should probably correct that.
A few things jump out at me:
You never assign the output of part1(x,a) to anything. You're told that
Results should be stored in a solution matrix using a different row of the solution matrix for each value of a.
What I take this to mean is that the 1st row corresponds to part1() evaluated for the 1st element of a. Since we're operating on x which is a vector, that row will have multiple columns. Your output is indeed a matrix. In your case, length(-pi:0.1:pi) == 63, therefore size(y) == [4 63], where y is your output matrix.
Your for loop is backwards. You're told to accept scalar a and vector x. Therefore, your script should be something like:
a = 0.5:0.5:2;
x = -pi:0.1:pi;
for i = 1:length(a)
y(i,:) = part1(x, a(i));
end
Note the use of length and the : operator. I want to iterate between 1 to length(a) (in this case, length(a) == 4) so that I can use the current a(i) value as an index into my output matrix, y. The : operator in y(i,:) signifies "The ith row and all columns of y will take the value output by part1(x,a(i))."
Your function needs to be changed up for element-by-element operations. Notice that, for instance, x*sin(a*x-2) works for scalar x but not vectors. This is because x is a vector and sin(a*x-2) is also a vector (since the sin call will operate element-by-element and a is a scalar). Trying to multiply two vectors together will result in errors since MATLAB will try to perform a matrix multiplication. Resolve this by replacing * with .*. This way it is unambiguous that you are going to multiply these two vectors element-by-element. You'll also need to change / to ./.
On another note, thank you for attempting to do your homework before asking SO for help. We've been getting a huge influx of questions from students that have made no attempt to do their own work before dumping it on us, so it's refreshing that we regulars of the MATLAB tag get to actual help out instead of telling people to do their own work.
Finally got the whole thing worked out.
Function
function [y] = part1(x,a)
y=((x.*sin(a.*x - 2))./(sqrt(1 + (a.*x).^2)));
end
Script
%%
clear all;
clc;
close all;
x=[-pi:.1:pi];
a=[.5:.5:2];
for i=1:length(a)
y(i,:)=part1(x,a(i));
plot(x,y)
end
Sol=[y]
I write a function to sum each row of a matrix which have three rows.
Then use a matrix which have one row and three columns to divide the previous result.
But I keep getting that error. I know the subscript should not be a decimal or negative number. But I still can not find the culprit. Please help, thanks.
% mean_access_time(ipinfo_dist, [306, 32, 192])
% 'ipinfo_dist' is a matrix which have three rows and column is not fixed.
function result = mean_access_time(hash_mat, element_num)
access_time_sum = sum(rot90(hash_mat));
result = bsxfun (#rdivide, access_time_sum, element_num);
For example:
A=
1 2
3 4
5 6
B= 7 8 9
Then I want to get
[(1+2)/7, (3+4)/8, (5+6)/9]
Update:
>> which rot90
/lou/matlab/toolbox/matlab/elmat/rot90.m
>> which sum
built-in (/lou/matlab/toolbox/matlab/datafun/#uint8/sum) % uint8 method
Culprit:
I used mean_access_time as a variable in the previous command line.
It seems like you have overridden a built-in function ( rot90 or sum ) with variable name.
Type
>> dbstop if error
And run your code.
When the error occurs type
K>> which rot90
K>> which sum
See if you get a built-in function or a variable name.