I have a little confusion about the poslin() function in octave . If I give in poslin(-1) , the output is the desired 0 ; and for poslin(n) , the output is n . However if I give in poslin( [ 3 ; -1]) ; the result is not [3 ; 0] ; which is somewhat expected , since the input to the poslin() function is not a scalar , but a vector . Now , if so ; how is it possible to convert [3 ; -1] to [3 ; 0] using poslin() function . Any suggestion will be helpful .
You can easily define your own vectorized poslin, e.g. poslin = #(x) x .* (x > 0)
Related
Question:
Consider the Initial value problem $$x'(t) = t*x^2$$ and $x(0)=1$
Solve this problem using ode45 for $ 0 \le t \le 1 $ and plot the result. And try solving again for $ 0 \le t \le 2 $
My matlab code for t in [0, 1] is as follows:
function f = fun( t, x )
f = t*x.^2;
end
>> [tv1 f1] = ode45 ('fun', [0 1], 1);
>> plot(tv1, f1)
And I get the following graph
However, when I run this Matlab code for t in [0, 2]
function f = fun( t, x )
f = t*x.^2;
end
>> [tv2 f2] = ode45 ('fun', [0 2], 1);
>> plot(tv2, f2)
I get the following graph,
Why this last graph for t in [0, 2] is wrong? How can I correct it?
Many thanks for your helps!
I created a function file on Octave like this,
function y = sum_odd(n)
if rem(n,2) ==1
sum_odd = sum(n)
endif
endfunction
and I want to make the function evaluate the sum of odd numbers in every vectors.
But when I tried sum_odd([1 2 3]), it didn't calculate for me.
Any ideas how to fix this problem?
You can simply use logical indexing:
function y = sum_odd(n)
y = sum(n(rem(n,2)==1))
end
rem([1 2 3],2)==1) returns logical array: [1 0 1].
n(logical([1 0 1])) returns[1 3]`.
sum(n(rem([1 2 3], 2) == 1)) returns sum of [1 3].
Here is what I want, a 3-D matrix:
K = 2:2.5:10;
den = zeros(1,4,4);
for i = 1:1:4
den(:,:,i) = [1, 5, K(i)-6, K(i)];
end
Or, a cell array is also acceptable:
K = 2:2.5:10;
for i = 1:1:4
den{i} = [1, 5, K(i)-6, K(i)];
end
But I want to know if there is a more efficient way of doing this using vectorized code like:
K = 2:2.5:10;
den = [1, 5, K-6, K];
I know the last code will not get what I wanted. But, like I can use:
v = [1 2 3];
v2 = v.^2;
instead of:
v = [1 2 3];
for i = 1:length(v)
v(i) = v(i)^2;
end
to get the matrix I want. Is there a similar way of doing this so that I can get the 3-D matrix or cell array I mentioned at the beginning more efficiently?
You need to "broadcast" the scalar values in columns so they are of the same length as your K vector. MATLAB does not do this broadcasting automatically, so you need to repeat the scalars and create vectors of the appropriate size. You can use repmat() for this.
K = 2:2.5:10;
%% // transpose K to a column vector:
K = transpose(K);
%% // helper function that calls repmat:
f = #(v) repmat(v, length(K), 1);
%% // your matrix:
den = [f(1) f(5) K-6 K];
This should be more optimized for speed but requires a bit more intermediary memory than the loop does.
Just use reshape with a 1*3 size:
den = reshape([ones(1,length(K));ones(1,length(K))*5; K-6; K],[1 4 length(K)]);
I think the used extra memory by reshape should be low and constant (dependent only on the length of the vector of new sizes).
You can use the classic line equation y=a*x+b, extended to the matrix form:
k = 2:2.5:10 ;
fa = [0 0 1 1].' ; %' // "a" coefficients
fb = [1 5 -6 0].' ; %' // "b" coefficients
d(1,:,:) = fa*k + fb*ones(1,4) ;
The above is better for clarity, but if you're not bothered you can also pack everything in one line:
d(1,:,:) = [0 0 1 1].' * (2:2.5:10) + [1 5 -6 0].' * ones(1,4) ;
If you need to re-use the principle for many different values of k, then you can use an anonymous function to help:
fden = #(k) [0 0 1 1].' * k + [1 5 -6 0].' * ones(1,4) ; %// define anonymous function
k = 2:2.5:10 ;
d(1,:,:) = fden(k) ; %// use it for any value of "k"
For given vector with values from y0 to yT I have to create following matrix:
1 y(P-1) y(P-2) ... y(0)
1 y(P) y(P-1) ... y(1)
1 y(P+1) y(P) ... y(2)
. . . ... .
. . . ... .
. . . ... .
1 y(T-1) y(T-2) ... y(T-p)
P means y(P) is a linear combination of P previous y's.
P is given (for example 100).
Is there any matlab function for this?
function to get the right y for given i,j is
getAIJ = #(i, j) y(p+(-(i+1))+(j-1));
So I just need to apply this function for every cell in matrix except the first column. Is there any matlab-way to do this? I am not sure how to use arrayfun on this.
Use buffer in signal processing toolbox. In this case
yy = buffer(y, P, P-1)';
I want to write a function, preferably an anonymous one, that filters an array. It's hard to say in words, but it's like this:
f = #(x) { if (x > 1) x+1 };
a = [ 1, 2, 3];
f(a) %
==> [ 1 3 4]
The key points I want is:
want the function to receive a number
or a string
but when given an array,
the function will apply itself to
each element in the array and returns
another array.
This is similar to the way the function log() works:
>> log(1)
ans =
0
>> log([1,2,3])
ans =
0 0.6931 1.0986
Thank you
You could just do this:
f = #(x) x + (x > 1);