How can I solve an equation and then save the variables in the workspace
7 views (last 30 days)
Show older comments
I want to solve an equation for a variable in a 'for' loop and then save the variable in the matlab workspace so that I can use the variable for use in further equations.
2 Comments
James Tursa
on 2 Jul 2014
Please provide a simple example or outline of what you need. Typically one might use cell arrays for this (i.e., C{1}, C{2}, etc instead of C1, C2, etc)
Accepted Answer
John D'Errico
on 2 Jul 2014
vpasolve returns a result, however you are just dumping to the command line.
Note the differences in what I did. There are several changes, and they are important.
test=[1:0.2:2];
n = numel(test);
syms x
sym('result',size(test));
for i=1:n
result(i) = vpasolve(test(i)*gamma(x)+4==gamma(x)*gamma(x),x);
end
result
result =
[ 3.2547273705290590128593428010284, 3.3016632430994950648588106024263, 3.3472332088911943448750737352559, 3.391437577553390151196553596555, 3.4342837920569247618248679670185, 3.4757861221877745059879809666923]
4 Comments
John D'Errico
on 2 Jul 2014
I would also point out that you could have used tools like arrayfun or cellfun to solve problems like this. However a loop is just as good. I think too often we look for the one line, "vectorized" solution in MATLAB, at a great cost in readability.
More Answers (0)
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!