MATLAB Answers

How do I declare varibles whose outputs are expected from Matlab simply without so many rules, like for example in c++?

108 views (last 30 days)
[Below is my code its meant to do as follows, as long as k<NoS, it shall form Equations that at the end of the day are equivalent to k =NoS. Afterwards, it should sum them up, and do fminsearch for Xcal Ycal Zcal and t0. My problem is however I declare the variables Xcal,Ycal,Zcal and t0 whether using syms Xcal, Ycal,Zcal, t0 or Xcal=(1,1,'double') ...etc. Matlab always displays the following "error Conversion to double from function_handle is not possible.". I am new to matlab, but I think there are just too many rules about multiplication and division. Whichever way I do it, there are always errors chiefly '.*' is not allowed for cell variables or sometimes '*' operand is not allowed for this kind of variable and so on and so forth. I've written a separate function on a different.m.file which can run, but I am just hoping to keep things simple and operate within one script, using an anonymous function as indicated. Also I am not very farmiliar with how to call functions or variables across scripts. I am not sure if what I am doing is correct or could someone let me know what I am not doing right thanks]
.....
while k<NoS;
k=1:NoS;
Xcal={};
Ycal={};
Zcal={};
t0={};
eqn=zeros(1,NoS);
eqn(k)=........
@(Xcal,Ycal,Zcal,t0)(sqrt(xj*xj-2*xj*Xcal+Xcal*Xcal+yj*yj-2*yj*Ycal+Ycal*Ycal+zj*zj-2*zj*Zcal+Zcal*Zcal)-c*tj+c*t0)
x0=[500,1000,50,5];
s2=sum(eqn(k));
soln =fminsearch(s2,x0)
break;
end
.....
  4 Comments
Alphonce Owayo
Alphonce Owayo on 2 Mar 2021
It does because I just highlighted the part of the code with the issue, but A1 B1, C1 and D1 are arrays of size(k) : so xj=B1(k), yj=C1(k), zj=D1(k) and tj=A1(k)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Mar 2021
You are trying to construct a vector of function handles. You cannot do that in MATLAB. This is because in MATLAB, () indicates both indexing and function invocation. If you had a vector eqn of function handles, would eqn(1) indicate indexing to retrieve the first function handle, or would it indicate that each element of the vector was to be invoked with parameter value 1?
Because of this, function handles must be stored in cell arrays, and then it becomes clear that {} indexes which one.
You also try to sum() the function handles. What is your expectation when you add function handles? Especially ones that might not have the same number of parameters or the same parameter names or which might have bound in different values of variables
A=1
f1 = @(x)x+A
A=2
f2 = @(x)x+A
f1+f2
is what exactly? It could not be value, but if it is a function handle then it has to bind in two different A simultaneously.
  19 Comments
Alphonce Owayo
Alphonce Owayo on 8 Mar 2021
i<ne means, its been executed we already have its Xcal Ycal zcal and t0 recorded so its fine. This is why the condition to stop is if i>=ne(1.5million), break from the loop and stop. I have done that with a few data points, it seems to pick everything correctly and returns quite reasonable, but results but the speed/efficiency. i=1;ne, should be i=1:ne;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!