how can we take the derivative of something then use it in an expression like
g := t->diff(f(t),t);
this fails because maple does not first take the derivative then apply t, but applies the value of t then tries to differentiate with respect to that value symbolically.
To solve this I usually have to precompute the differential, then copy and paste it. The problem with this method is that any time the original function changes, everything needs to be redone, which can be time consuming.
Is there a better way?
Using the D operator you can assign the "derivative" of the operator f to g, without having first assigned anything to f.
You can subsequently assign and reassign different operators to f, after which g can be called. Calling g in the example below will not cause f to be called with any symbolic argument.
restart;
g := D(f);
g := D(f)
f := x->sin(x)+x^2:
g(x);
cos(x) + 2 x
g(2);
cos(2) + 4
f := x->tan(x)+x^3:
g(x);
2 2
1 + tan(x) + 3 x
g(2);
2
13 + tan(2)
Another way to do it is as follows. Note that with this methodology every call to g will cause Maple to call diff on the symbolic local T. If -- after assigning to f -- you call g many times then that will be more expensive (even if just for the overhead of checking a memoized result and making the extra function call to diff). Also, your f may be something which is not even set up to return appropriately for a non-numeric argument, so the call to f(T) might be problematic.
restart;
g := proc(t) local T; eval(diff(f(T),T),T=t); end proc:
f := x->sin(x)+x^2:
g(x);
cos(x) + 2 x
g(2);
cos(2) + 4
f := x->tan(x)+x^3:
g(x);
2 2
1 + tan(x) + 3 x
g(2);
2
13 + tan(2)
The D operator is pretty useful. You could check out its help page.
Related
I am new with Octave and I have a problem.I thought the following codes were the same, but they produces different results. What is the difference? Thanks
Octave/Matlab: Difference between e^(-1*z) and exp(-1*z)
g = 1./(1 + e^(-1*z));
g = 1./(1 + exp(-1*z));
Where z is a vector, element or matrix
In Octave
exp(1) equals e where e is Euler's number.
There are 4 operations/functions that are to be noted here:
e^x is same as expm(x) and e.^(x) is same as exp(x).
e^x and expm(m) represent e raise to the matrix x.
e.^(x) and exp(x) represent exponential ex for each element in matrix x.
If x is a scalar then all (e^x, expm(x), e.^x and exp(x)) are mathematically equal.
For your case, z is a matrix and hence you get different results.
In MATLAB,
e is not defined in MATLAB. exp(x) and expm(x) have same definitions in MATLAB as those that are described for Octave above.
PS: e or E are also used for E-notation in both MATLAB and Octave but that's a different thing.
In Octave, it is important to note that e^x and exp(x), where x is a double precision scalar variable, are not necessarily the same.
For instance:
>> a = e ^ 2
a = 7.3891
>> b = exp (2)
b = 7.3891
>> b - a
ans = 8.8818e-16
The reason is that exp (2) uses a dedicated algorithm to compute the exponential function, while e ^ 2 actually calls the function e () to get the value of e, and then squares it:
>> c = realpow (e (), 2)
c = 7.3891
>> c - a
ans = 0
Another reason why e ^ x and exp (x) differ is that they compute completely different things when x is a square matrix, but this has already been discussed in Sardar's answer.
I defined an inline function f that takes as argument a (1,3) vector
a = [3;0.5;1];
b = 3 ;
f = #(x) x*a+b ;
Suppose I have a matrix X of size (N,3). If I want to apply f to each row of X, I can simply write :
f(X)
I verified that f(X) is a (N,1) vector such that f(X)(i) = f(X(i,:)).
Now, if I a add a quadratic term :
f = #(x) x*A*x' + x*a + b ;
the command f(X) raises an error :
Error using +
Matrix dimensions must agree.
Error in #(x) x*A*x' + x*a + b
I guess Matlab is considering the whole matrix X as the input to f. So it does not create a vector with each row, i, being equal to f(X(i,:)). How can I do it ?
I found out that there exist a built-in function rowfun that could help me, but it seems to be available only in versions r2016 (I have version r2015a)
That is correct, and expected.
MATLAB tries to stay close to mathematical notation, and what you are doing (X*A*X' for A 3×3 and X N×3) is valid math, but not quite what you intend to do -- you'll end up with a N×N matrix, which you cannot add to the N×1 matrix x*a.
The workaround is simple, but ugly:
f_vect = #(x) sum( (x*A).*x, 2 ) + x*a + b;
Now, unless your N is enormous, and you have to do this billions of times every minute of every day, the performance of this is more than acceptable.
Iff however this really and truly is your program's bottleneck, than I'd suggest taking a look at MMX on the File Exchange. Together with permute(), this will allow you to use those fast BLAS/MKL operations to do this calculation, speeding it up a notch.
Note that bsxfun isn't going to work here, because that does not support mtimes() (matrix multiplication).
You can also upgrade to MATLAB R2016b, which will have built-in implicit dimension expansion, presumably also for mtimes() -- but better check, not sure about that one.
This is for my course in numerical methods. I am trying very hard to understand MATLAB and its syntax but I am 100% self-taught, so please bear with me if my attempts seem ridiculous.
I have written this very easy function to approximate the number e
function e= calcEulerLimit(n)
e = (1 + 1./n).^n;
end
This is the 'basic' definition of the number e using a limit n to infinity approach. For MATLAB I defined the following vector (when I am talking about n in the latter, I am always referring to this vector n)
n=[1:1:10]=[ 1 2 3 4 5 6 7 8 9 10 ]
and the output works just fine as I expected, it is sensitive towards the vector n input when I call my function in MATLAB.
>> calcEulerlimit(n)
ans =
2.0000 2.2500 2.3704 2.4414 2.4883 2.5216 2.5465 2.5658 2.5812 2.5937
Now I want to do the exact same as above with a Taylor Approach, using the infinite summation formula to describe e, here is where I am stuck, the following simple code works:
function e = calcEulerSum(n)
e=1; % base-case, start variable
for i=1:1:n % for loop with step size one
e=e+1/factorial(i)
end
end
but this input, of course, does not work when I want to enter a vector such as n that computes through all variables.
I tried something along the line with another for loop, and a while loop, but the while loop seems to never terminate:
function e = calcEulerSum3(n)
while n
e=1;
e = e + 1./cumprod(n);
end
end
Using cumprod(n) to get the factorial value of each column element of my vector n.
You're trying to vectorize your function. Your solution for scalars works so let's look at what it's doing as i is incremented:
e0 = 1;
i = 1: e(1) = e0 + 1/factorial(1)
i = 2: e(2) = e(1) + 1/factorial(2) = e0 + 1/factorial(1) + 1/factorial(2)
= e0 + sum(1./factorial(1:2))
i = 3: e(3) = e(2) + 1/factorial(3) = ...
= e0 + sum(1./factorial(1:3))
...
i = n: e(n) = e(n-1) + 1/factorial(n) = e0 + 1/factorial(1) + ... + 1/factorial(n)
= e0 + sum(1./factorial(1:n))
So can you come up with a general expression to calculate the vector e given n? The cumsum function will come in handy.
for i=whatsoever,statement(i);end executes the statement on every element of whatsoever. If it is a single number, then on this one, if it is a vector/array, then on every element of it.
1:1:n creates an array of integers from 1 to n on the spot (1:n would do, too). If n already is a vector with the elements you want to iterate over, you can use it directly: for i=n.
However, why did you use the dotted versions of operations in your first and third code block, but not your second? Because you read about MATLAB's vectorization? Then you seem to be on the right track, but remember that the point vectorization is to get rid of an explicit loop entirely.
function e = calcEulerSum(n)
e=nan(1,length(n)); % initialize to nan
for j=1:length(n) % for each element in the input
e(j)=sum(1./factorial(0:n(j))); %each entry is computed in this step, one at a time
end
In this code, each approximation has been vectorized, but I don't see an easy way to vectorize the entire program. There is also no error checking, like making sure the elements of n are non-negative integers, or that n is a vector and not an array.
In the original example, the while never terminates because n never changes.
This should be enough to help get you started.
Cheers!
I have this equation: p = (1 + (q-1)*B*T) ^ (-1/q-1)
The values p, T are known and the diagram p - T makes curbe. I want to calculate q and B so that curbe be as close to straight line is posible.
Some values are:
T p
1 0,999147061
2 0,997121331
3 0,994562513
Is there any way to make matlab (or sth else) to give me the values of B and q ?
Please help me write a MATLAB program that constructs a column matrix b, such that
b1 = 3x1 - 3/4y0
b2 = 3x2
...
bn-2 = 3xn-2
bn-1 = 3xn-1 - 3/4yn
where x and y are variables. Notice that y only appears in the first and last entries of b.
My problem is that I don't know how variables work in MATLAB. I tried
b = 3*x
and it says
??? Undefined function or variable 'x'
So, how do we create variables instead of constants?
Thanks!
EDIT:
From your comments above, what you need is MATLAB's symbolic toolbox, which allows you to perform computations in terms of variables (without assigning an explicit value to them). Here's a small example:
syms x %#declare x to be a symbolic variable
y=1+x;
z=expand(y^2)
z=
x^2 + 2*x + 1
You will need to use expand sometimes to get the full form of the polynomial, because the default behaviour is to keep it in its simplest form, which is (1+x)^2. Here's another example to find the roots of a general quadratic
syms a b c x
y=a*x^2+b*x+c;
solve(y)
ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
I think you meant bn and xn in the last line... Anyway, here's how you do it:
b=3*x;
b([1,end])=b([1,end])-3/4*y([1,end])
You can also do it in a single line as
b=3*x-3/4*[y(1); zeros(n-2,1); y(end)];
where n is the length of your vector.
You never stated your problem...
Anyways just set the first entry of b individually first. Then use a loop to set the next values of b from 2 up to n-2. Then set the last entry of b individually.
On a side note, if x is a vector, you can simply vectorize the loop part.