vo is the upper limit of an integration expression and this is the variable that I want to predefine as a variable without value since it is the variable I am trying to solve for.
Everything else is defined.
%//a object's velocity is changing as a function of sine, the full cycle is 0=>speed=>0
vt=-((speed/2)*cos((2*pi/t0)*t)-(speed/2)))
%//integrate vt function so I can get distance which has given
distance=int(vt,t,0,t0)
%//the last step is I need to find how long does it takes the object to finish a full cycle of movement
time=solve(eqn,t0)
Try using the syms command in MATLAB. The syms is part of the symbolic math toolbox and you can predefine "variables" without explicitly solving for them. They form a part of the mathematics expression that you want.
Once you formulate the equation you want, you can use solve (as you have already used in your post) to solve the variable for you.
Related
I have a problem that needs to use ode45 in order to find the solution, but one of the state variables is defined using the derivative of another function.
The code I have for this is:
`[~,s]=ode45(#eom,time,IC,options);
function sdot=eom(~,s)
mu=0.01215510;
C=3;
x=s(1);
y=s(2);
gamma=s(3);
O=0/2*(x^2+y^2)+(1-mu)/sqrt((x+mu)^2+y^2)+mu/sqrt((x+mu-1)^2+y^2);
m=sqrt(2*O-C);
sdot=[m*cos(gamma);m*sin(gamma);(Oy*cos(gamma)-Ox*sin(gamma))/sqrt(2*O-C)-2];
sdot=sdot';
end`
Where Ox=dO/dx, and Oy=dO/dy.
However, I have tried putting it into a separate function file and several other methods, but I cannot figure out how to take the derivative of O with respect to x and y, and then transfer those x and y values back into the state defined variables.
In the separate function, I defined Od=O(mu,d), where d would determine what variable the derivative would be taken with respect to. Within this function, I defined it as Od(a,b)=diff(O,d,1). I had thought that I could input this function into my state function where I could define it as Od(s(1),s(2)), but this only returning error messages.
I would like to find optimal hyperparamters for a specific function, I am using bayesopt routine in MATLAB.
I can set the variables to optimize like the following:
a = optimizableVariable('a',[0,1],'Type','integer');
But I have coupled variables, i.e, variables whose value depend on the existence of other variables, e.g., a={0,1}, b={0,1} iff a=1.
Meaning that b has an influence on the function if a==1.
I thought about creating a unique variables that encompasses all the possibilities, i.e., c=1 if a=0, c=2 if a=1,b=0, c=3 if a=1,b=0. The problem is that I am interested in optimizing continuous variables and the above approach does not hold anymore.
I tried something alone the line of
b = a * optimizableVariable('b',[0,1],'Type','integer');
But MATLAB threw an error.
Undefined operator '*' for input arguments of type 'optimizableVariable'.
After three months almost to the day, buried deep down in MATLAB documentation, the answer was to use constrained variables.
https://www.mathworks.com/help/stats/constraints-in-bayesian-optimization.html#bvaw2ar
I am currently having an equation that contains 4~5 variables, and I am unfamiliar with matlab. I want to see how a variable change with the other variable fixed (might assign them some value). Maying graphing them out will help. I found it cumbersome to work in the main GUI in matlab, this is mainly after I type in my equation, I didn't know if the expression is what I want because of missing brackets, etc.
Recently I found mudpad, it is better because it will show you the expression in pretty form. I am wondering is there any other tool-box that is more suitable for my intention?
Update:
Ps=1;
Pr=1;
Pd=1;
g=0.25;
d1=1;
d2=1;
n=0.18;
Yr = #(x)(Ps)/(d1*((Pd*g^2)/d2 + (n*(x- 2))/(x- 1)));
Yr_=ezplot(Yr,0,1);
There is actually 4 more equations to plot and I am only posting one of them.
This question may initially appear similar to this other question but my situation is a little bit different.
I have a function 'deriv' that takes a symbolic expression as its input, then takes the first derivative of that symbolic expression. That derivative is then converted into an anonymous function using matlabFunction(), and is then evaluated over an array of points. I also use the anonymous function later in some other code.
The problem I'm having is that sometimes the input symbolic expression happens to be linear, and thus the derivative is constant; therefore the anonymous function is also a constant. When I evaluate the anonymous function over the array of points, I only get one output instead of an array of outputs.
Here's some code showing what I'm doing. For the sake of simplicity here, let's assume that the symbolic input expressions will involve only one symbolic variable called q.
function[derivFun,derivVals] = deriv(input)
derivSym = diff(input,q);
derivFun = matlabFunction(derivSym,'vars',q);
evalPoints = [1;2;3;4;5]; %in my true application, a much larger array
derivVals = derivFun(evalPoints);
end
So if the input is q^2, then the output derivVals will be [2;4;6;8;10]. But if the input happens to be, say, 3*q, then derivVals will be 3 (just a single scalar). What I'd like is for derivVals to be [3;3;3;3;3].
That is, I'd like derivVals to be the same size as evalPoints even if the input function happens to be linear (or constant). And I don't know ahead of time what the input expression will be.
Can anyone give suggestions for a scheme that would do that? I understand that a constant anonymous function will just return a single constant scalar, regardless of the size of its input. What I'm hoping for is perhaps some way to recognize when the anonymous function is constant and then still cause derivVals to be the same size as evalPoints.
I know that I could use a for loop to evaluate derivFun for every row of evalPoints, but I'd like to avoid using such a loop if possible.
Thank you for your time and consideration.
I think that this is a slightly simpler solution. The issue is that you're using matlabFunction, which simplifies down the equations and doesn't allow much customization. However, you can create an anonymous function of an anonymous function. Just add the this line right after your matlabFunction line:
derivFun = #(evalPoints)derivFun(evalPoints)+zeros(size(evalPoints));
This only evaluates the original derivFun once. However, I do like you symvar solution (just remember that adding zeros is always better than multiplying ones).
Not 100% sure I got the problem correctly.
Would this solve your issue?:
if isscalar(derivVals)
derivVals = repmat(derivVals, size(evalPoints));
end
I'm trying to parameterize one of my Simulink models, so that I will have a gain in the model whose value is equal to an element of a MATLAB workspace vector indexed by the model parameter. That is, I want to define a model argument WheelIndex and have a gain inside the model with a value AxelLoads(WheelIndex).
When I do it exactly as I've described above, I get "vector indices must be real and positive integers" error. When I change the model argument to AxelLoad(To be used directly in the gain component) and assign its value to be AxelLoads(1)(for the first wheel) I get:
Error in 'Overview/Wheel1'. Parameter '18000.0, 15000.0, 17000.0,
21000.0' setting: "18000.0, 15000.0, 17000.0, 21000.0" cannot be evaluated.
I've also tried importing the vector as a constant block into the model, and use a selector block parameterized by the WheelIndex argument to direct the right element to a multiplication block (thereby making an ugly gain block), but then Simulink complains that I'm trying to use a model argument to define an "un-tunable value".
I just want to somehow define the parameters in the MATLAB workspace to be used in each model instance, so that I can, say, calculate the total weight by adding the loads on each wheel. Simulink seems to block all workarounds I've been trying.
Thanks
Could you use a lookup table to obtain AxelLoads vs. WheelIndex?
The easiest way is if I just came over? :P
Perhaps this explaination of tunable parameters helps a little?