How to obtain a no-loop iteration in MATLAB using anonymous functions?

I tried to adapt this base program to my needs, but I am running into an issue. In my particular program I have my function F dependent on other functions. Would you have suggestions on how to incorporate the same idea if the program was modified to include
q = sym('q',[1,10]);
i = 1:10;
M = @(q) tan(q)/(1+sin(7*q))
N = log(M(q))
F = i - 2*q*N == 0;
solq = solve(F,q)
I obtain the same errors in this program that I do in my actual program. I am informed that MATLAB is doing away with the sym('q',...) style argument in favor of 'syms q'. If I use this notation, everything ends up being symbolic, including the answer. I essentially want to solve the equation given in F. The general code works until I include an anonymous function. Is there a way around this?

 Accepted Answer

It appears that the error here is related to how MATLAB is interpreting the multiplication and division used. Since your data is stored as vectors, you need to use "./" and ".*" in place of "/" and "*", if I correctly understand what you are trying to do.
The code below resolves this issue. Note, however, that "solve" returns a numerical solution:
q = sym('q',[1,10]);
i = sym(1:10);
M = tan(q)./(1+sin(7*q))
N = log(M)
F = i - 2*q.*N == 0;
solq = solve(F,q)
Note that solves 10 equations for 10 independent unknowns.
Best Regards,
Jim

5 Comments

Jim, thanks for your response. I copied your code into MATLAB, but I receive the following error
Warning: 20 equations in 10 variables.
> In /usr/local/common/packages64/Matlab_2014a/toolbox/symbolic/symbolic/symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
In untitled3 at 6
Warning: Explicit solution could not be found.
> In solve at 179
In untitled3 at 6
solq =
[ empty sym ]
Do you know why this error occurs? Also, a numerical solution is fine and expected. I expect 10 different answers, one for each iteration of 'i'. After I get a handle on this, I was going to try to iterate the second variable 'n' in the equation, so that I then have 100 answers. That is, for every iteration of 'n' from 1:10, I would have 10 iterations of 'i' from 1:10. Ultimately, I would be able to like to run the program, and then type in the command window a command with a given 'i'-value and a given 'n'-value and get the solution to my function 'F' corresponding to those values. I hope this clears up what I am trying to ultimately accomplish.
This appears to be due to how MATLAB R2014a parses equations.
If you change the call to "solve" to the form shown below, the warning goes away, and a solution is returned:
>> solq = solve(F)
It does solve it now, but the output is now
solq =
q1: [1x1 sym]
q2: [1x1 sym]
q3: [1x1 sym]
q4: [1x1 sym]
q5: [1x1 sym]
q6: [1x1 sym]
q7: [1x1 sym]
q8: [1x1 sym]
q9: [1x1 sym]
q10: [1x1 sym]
The solutions of F should not be symbolic, as I should have actual values for q1...q10.
The answer is symbolic in the sense that the datatype of the solutions is a MATLAB 'sym'. That being said, the answers are numeric. You can see this by entering the command below:
>> solq.q1
ans =
- 227.2660704691069869919292425356 + 0.23260362927881213795953027706372i
The solutions are represented using 'vpa' (variable-precision arithmetic), which is the Symbolic Math Toolbox's way of representing numeric values. You can convert to 'double' types using the line of code below:
>> double(sol.q1)
ans =
-2.2727e+02 + 2.3260e-01i
Note that, as the name implies, numbers represented in vpa can have variable amounts of precision. Thus, there may be round-off error when converting to doubles.
Jim Joy, thanks a bunch. I was reading up on conversion from symbolic (https://www.mathworks.com/help/symbolic/conversion.html) prior to your answer.
I will pay attention to the MATLAB syntax between versions when I look up info on the site as well! Thanks for the lessons!

Sign in to comment.

More Answers (0)

Asked:

BM
on 1 Sep 2017

Commented:

BM
on 8 Sep 2017

Community Treasure Hunt

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

Start Hunting!