I am writing a code for some physics problem. I want to simply neglect the terms containing the variable 't' and make my matrix independent of time 't'.
syms t real
syms temp
i=sqrt(-1);
temp=[exp(t*2*i)*exp(-t*5*i) + 12 exp(3*t*i); exp(-3*t*i) exp(-t*2*i)*exp(t*5*i)+8];
what i expect in my out put is
temp= [12 0;0 8];
I am unable to do it. Can any one help me to solve this ?
Thanks in advance.
A couple of comments:
i is already supported as the imaginary number in MATLAB. It is superfluous to declare i = sqrt(-1);. Also real is a function in MATLAB. It basically returns the real part of a complex number defined in MATLAB. Do not make this symbolic. By doing this, you would be shadowing over the function with that variable name.
Don't do syms temp. You are making this variable symbolic, yet you are overwriting its behaviour in the last line of code.
Now, what you mean by "neglect" is to have any values that have t in the expression go to 0. There really isn't an easy way to do this. There's no utility in MATLAB (at least to my knowledge), that can pick through all mathematical expressions in an array or matrix where if there are terms that have a t in it, you should set them to 0.
The only thing I can suggest is if you substitute t = 0, then subtract 1 from all of the elements. This is because when you substitute t = 0 into each expression in your matrix, exp(0) = 1, and so all elements will have a common 1 term due to each expression having an exponential term. Therefore:
out = subs(temp, 't', 0) - 1;
out =
[ 12, 0]
[ 0, 8]
Related
I am trying to replicate this formula:
I have gathered all variables in my workspace. However estimating vec(Theta') does not seem to work and so I am a little bit stuck.
Theta = A*B-C;
vTheta = vec(Theta');
A, B and C are defined. The problem is that MATLAB does not seem to know the function vec to do what I would like to do with Theta as in the formula.
How to fix this?
I don't know where you got that equation from, but vec is a function in R, maybe it's related to that? If you want to convert a matrix Theta into a vector, do
Theta(:)
Edit: If you need to transpose the matrix first, MATLAB might not let you do Theta'(:). Instead do it in two steps:
tmp = Theta'; tmp(:)
As written above the Colon Operator is the way vectorize defined variable.
Yet, sometime we want to vectorize a sub set of a variable.
Let's say we have a matrix - mA and we'd like to vectorize a sub section of it - mA(2:3, 4:7).
One way is to define a new variable and vectorize it:
vA = mA(2:3, 4:7);
vA = vA(:);
Yet, what if we only wanted to use this inside another expression and only once?
Could we escape the need to generate explicit variable?
Well, unfortunately MATLAB doesn't have the view() functionality like in Julia.
Yet if you want to avoid explicitly naming new variable (I'm not sure if MATLAB's JIT Engine can also void the memory allocation as Julia) you can do:
reshape(mA(2:3, 4:7), [], 1)
This will always yield a column vector.
You can also use:
reshape(mA(2:3, 4:7), 1, [])
To generate row vector.
For instance you can do:
reshape(mA(2:3, 4:7), 1, []) * reshape(mA(2:3, 4:7), [], 1, )
This will be the sum of squared values of those elements.
I'm a bit of a beginner at matlab so I'm having a some trouble understanding differentiating a dot operator and a for loop.
Given a Column vector (it's a pretty long column vector). We are given the following equation...
f(x)=0.2*x^3 + (1/3)*(x^2-1) + 2*cos(x)+3*cos(10x)
I need to use the method of dot operator and a for loop to create 2 plots and also the time (using tic, toc)
However, with dot operator does it mean using
.^ or .*
in the equation? and if this is the case, wouldn't I still need to use that in order to make a for loop?
Any clarification or assistance would be greatly appreciated! I don't really understand how I would write these...
The operators prefixed with a dot are called element-wise operators. It performs the operation on each element of the arrays (after checking that all involved arrays have the same number of elements). So you don't need a for-loop with using this operator, this is implied. This is called vectorization.
For example:
C = A.*B;
is equivalent to:
C = zeros(size(C));
for i=1:numel(A)
C(i) = A(i)*B(i);
end
but the first one is heavily optimized. So it's strongly advised to use vectorized operators as much as possible.
your x is an vector of defined length and step size, for example it can be:
x = 1:1:100 %generates 1,2,3....100
x = 1:0.1:10 %generates 0.1,0.2,0.3....10
so if you want to write a function of x (which is a vector), for speed purposes, you might want to use the dot-product denoted by .* in matlab. In your case you can do:
f(x)=0.2.*x.^3 + (1/3).*(x.^2-1) + 2.*cos(x) +3.*cos(10x)
The more costly way to compute is to use a for loop:
for x = 1:100
f(x)=0.2*x^3 + (1/3)*(x^2-1) + 2*cos(x) +3*cos(10x)
end
figure
plot(f)
The problem in your for loop is that you overwrite on the value of fx2, in each iteration you give it a new value but it always remain of size 1x1. For minimal change in your code you could do something like:
fx2=[];
for x = A(:,4)
fx2 = [fx2 0.2*x^3+(1/3)*(x^2-1)+2*cos(x)+3*cos(10*x)];
end
plot(x,fx2)
that way, you add a new value in the vector fx2 at each iteration instead of overwriting, (fx2 would be 1x1 then 1x2...). However be aware that this is not optimized at all because well there is a for loop that can be avoided but also because fx2's size changes at each iteration. Another better solution would be to predefine fx2 with the right size and then in the loop, change its ith value at the ith iteration.
I wanted to solve the following equation in MATLAB R2013a using the Symbolic Math Toolbox.
(y/x)-(((1+r)^n)-1)/r=0 where y,x and n>3 are given and r is the dependent variable
I tried myself & coded as follows:
f=solve('(y/x)-(((1+r)^n)-1)/r','r')
but as the solution for r is not exact i.e. it is converging on successive iterations hence MATLAB is giving a warning output with the message
Warning: Explicit solution could not be found.
f =
[ empty sym ]
How do I code this?
There are an infinite number of solutions to this for an unspecified value of n > 3 and unknown r. I hope that it's pretty clear why – it's effectively asking for a greater and greater number of roots of (1+r)^n. You can find solutions for fixed values of n, however. Note that as n becomes larger there are more and more solutions and of course some of them are complex. I'm going to assume that you're only interested in real values of r. You can use solve and symbolic math for n = 4, n = 5, and n = 6 (for n = 6, the solution may not be in a convenient form):
y = 441361;
x = 66990;
n = 5;
syms r;
rsol = solve(y/x-((1+r)^n-1)/r==0,r,'IgnoreAnalyticConstraints',true)
double(rsol)
However, the question is "do you need all the solutions or just a particular solution for a given value of n"? If you just need a particular solution, you shouldn't be using symbolic math at all as it's slower and has practical issues like the ones you're experiencing. You can instead just use a numerical approach to find a zero of the equation that is near a specified initial guess. fzero is the standard function for solving this sort of problem in a single variable:
y = 441361;
x = 66990;
n = 5;
f = #(r)y/x-((1+r).^n-1)./r;
r0 = 1;
rsol = fzero(f,r0)
You'll see that the value returned is the same as one of the solutions from the symbolic solution above. If you adjust the initial guess r0 (say r0 = -3), it will return the other solution. When using numeric approaches in cases when there are multiple solutions, if you want specific solutions you'll need to know about the behavior of your function and you'll need to add some clever extra code to choose initial guesses.
I think you forgot to define n as well.
f=solve('(y/x)-(((1+r)^n)-1)/r=0','n-3>0','r','n')
Should solve your problem :)
I have a matrix of numbers for one of the variables in an fsolve equation so when I run matlab I am hoping to get back a matrix but instead get a scalar. I even tried a for loop but this gave me an error about size so that is not the solution. I am including the code to get some feedback as to what i am doing wrong.
z=0.1;
bubba =[1 1.5 2];
bubba = bubba';
joe = 0:0.1:1.5;
joe = repmat(joe,3,1);
bubba = repmat(bubba,1,length(joe));
for x=1:1:16
eqn0 = #(psi0) (joe.-bubba.*(sqrt((psi0+z))));
result0(x) = fsolve(eqn0,0.1,options);
end
note I need the joe variable later for plotting so I clipped that part of the code.
Based on your earlier comments, let me take a shot at a solution... still not sure this is what you want:
bubba =[1 1.5 2];
joe = 0:0.1:1.5;
for xi = 1:numel(joe)
for xj = 1:numel(bubba)
eqn0 = #(psi0) (joe(xi).-bubba(xj).*(sqrt((psi0+z))));
result(xi,xj) = fsolve(eqn0,0.1,options);
end
end
It is pedestrian; but is it what you want? I can't access matlab right now, otherwise I might come up with something more efficient.
To elaborate on my comment:
psi0 is the independent variable in your solver. You set the dimension of it to [1 1] when you use a scalar as the second argument of fsolve(eqn0, 0.1, options); - this tells Matlab to optimize the scalar psi0, starting at a value of 0.1. The result will be a scalar - the value that minimizes the function
0.1 * sqrt(psi0 + 0.1)
since you had set z=0.1
You should get a value of -0.1 returned for every iteration of your loop, since you never changed anything. There is not enough information right now to figure out which factor you would like to be a matrix - especially since your expression for eqn0 involves a matrix multiplication, it's hard to know what you expect the dimensionality of the result to be.
I hope that you will use this initial answer as a springboard to modify your question so it can be answered properly!?
usually, symbolic functions return vectors for vector inputs:
syms('x');
f=x*2;
subs(f,[1 2 3])
outputs: [2 4 6]
but doing
f=sym('0');
subs(f,[1 2 3]);
outputs: 0
and not: [0 0 0]
so basically, my questions is how do I make f behave as a "normal" symbolic function.
I can do something ugly like f=x-x to create a function that always returns zero, but is there a prettier way?
Actually, there is no way.
sym('0') creates a symbolic constant (0, in this case). subs() replaces all variables with each value from the given vector. However, you have no variables, so subs() just returns the given symbolic constant.
It gets better. sym() internally does some simplification, so sym('0*x') or sym('x-x') both become sym('0') and you get the exact same behavior. Similarly, sym('x/x') turns into sym('1') and you just get the scalar 1 back from subs() even if you pass it a vector.
The only way you're going to get around this is by writing a helper function that detects if the size() of the output from subs() is less than the vector, and turns it into the correct size of a vector if needed.
I don't have the Symbolic Toolbox so this is a blind guess. Your function declarations don't look identical enough:
syms('x');
f=x*2;
against
f=sym('0');
Perhaps what you have done in the first case is define a function which returns the double of its inputs, in the second case defined a function which returns 0 whatever its inputs. Maybe
syms('x');
f=x*0;
is what you need ?