Using Solve function in a for loop

Hey everyone,
I'm trying to iterate solve function over a range of numbers (0 to 1500). but ends up in an error "Unable to perform assignment because the left and right sides have a different number of elements"
Any ideas?
An = 0.2;
Af = 0.125;
B = 2;
v = 0:15:1500;
syms dy
out = zeros(size(v));
for i=1:length(v)-1
volume = v(i) == B*0.2 + (3/5)*An*dy.^(5/3)/((1-(An/Af)^1.5)^(2/3));
out(i) = double(solve(volume,dy));
end

 Accepted Answer

The problem is that solve returns two solutions and you want to store them in a single element of out. Instead of doing that, you can make out a cell array so that each element of out is a vector with however many solutions were returned from solve on that iteration, and then combine all the solutions at the end (if you got the same number of solutions on each iteration):
An = 0.2;
Af = 0.125;
B = 2;
v = 0:15:1500;
syms dy
out = cell(size(v)); % cell array out
for i=1:length(v)-1
volume = v(i) == B*0.2 + (3/5)*An*dy.^(5/3)/((1-(An/Af)^1.5)^(2/3));
out{i} = double(solve(volume,dy));
end
% this will only work if every call to solve
% returned the same number of solutions:
out = [out{:}].'
out =
1.0e+02 * 0.0168 - 0.0122i -0.0208 + 0.0000i 0.0556 + 0.1712i -0.1456 - 0.1058i 0.0850 + 0.2616i -0.2225 - 0.1617i 0.1087 + 0.3345i -0.2845 - 0.2067i 0.1293 + 0.3981i -0.3386 - 0.2460i 0.1480 + 0.4554i -0.3874 - 0.2815i 0.1652 + 0.5084i -0.4324 - 0.3142i 0.1813 + 0.5578i -0.4745 - 0.3448i 0.1964 + 0.6045i -0.5143 - 0.3736i 0.2109 + 0.6490i -0.5520 - 0.4011i 0.2247 + 0.6914i -0.5882 - 0.4273i 0.2379 + 0.7322i -0.6229 - 0.4525i 0.2507 + 0.7716i -0.6563 - 0.4769i 0.2631 + 0.8096i -0.6887 - 0.5004i 0.2750 + 0.8465i -0.7201 - 0.5232i 0.2867 + 0.8823i -0.7506 - 0.5453i 0.2980 + 0.9172i -0.7803 - 0.5669i 0.3091 + 0.9513i -0.8092 - 0.5879i 0.3199 + 0.9845i -0.8375 - 0.6085i 0.3305 + 1.0170i -0.8651 - 0.6286i 0.3408 + 1.0489i -0.8922 - 0.6482i 0.3509 + 1.0801i -0.9188 - 0.6675i 0.3609 + 1.1107i -0.9448 - 0.6864i 0.3706 + 1.1407i -0.9704 - 0.7050i 0.3802 + 1.1703i -0.9955 - 0.7233i 0.3897 + 1.1993i -1.0202 - 0.7412i 0.3990 + 1.2279i -1.0445 - 0.7589i 0.4081 + 1.2561i -1.0685 - 0.7763i 0.4171 + 1.2838i -1.0921 - 0.7934i 0.4260 + 1.3111i -1.1153 - 0.8103i 0.4348 + 1.3381i -1.1383 - 0.8270i 0.4434 + 1.3647i -1.1609 - 0.8434i 0.4520 + 1.3910i -1.1832 - 0.8597i 0.4604 + 1.4169i -1.2053 - 0.8757i 0.4687 + 1.4426i -1.2271 - 0.8915i 0.4769 + 1.4679i -1.2487 - 0.9072i 0.4851 + 1.4929i -1.2700 - 0.9227i 0.4931 + 1.5177i -1.2910 - 0.9380i 0.5011 + 1.5422i -1.3119 - 0.9531i 0.5090 + 1.5664i -1.3325 - 0.9681i 0.5168 + 1.5904i -1.3529 - 0.9829i 0.5245 + 1.6142i -1.3731 - 0.9976i 0.5321 + 1.6377i -1.3931 - 1.0121i 0.5397 + 1.6610i -1.4129 - 1.0265i 0.5472 + 1.6841i -1.4326 - 1.0408i 0.5546 + 1.7070i -1.4520 - 1.0550i 0.5620 + 1.7296i -1.4713 - 1.0690i 0.5693 + 1.7521i -1.4904 - 1.0829i 0.5765 + 1.7744i -1.5094 - 1.0966i 0.5837 + 1.7965i -1.5282 - 1.1103i 0.5908 + 1.8184i -1.5468 - 1.1238i 0.5979 + 1.8402i -1.5653 - 1.1373i 0.6049 + 1.8617i -1.5837 - 1.1506i 0.6119 + 1.8831i -1.6019 - 1.1638i 0.6188 + 1.9044i -1.6200 - 1.1770i 0.6256 + 1.9255i -1.6379 - 1.1900i 0.6324 + 1.9464i -1.6557 - 1.2030i 0.6392 + 1.9672i -1.6734 - 1.2158i 0.6459 + 1.9879i -1.6910 - 1.2286i 0.6526 + 2.0084i -1.7084 - 1.2412i 0.6592 + 2.0287i -1.7257 - 1.2538i 0.6657 + 2.0490i -1.7429 - 1.2663i 0.6723 + 2.0691i -1.7600 - 1.2787i 0.6788 + 2.0890i -1.7770 - 1.2911i 0.6852 + 2.1089i -1.7939 - 1.3033i 0.6916 + 2.1286i -1.8107 - 1.3155i 0.6980 + 2.1482i -1.8273 - 1.3276i 0.7043 + 2.1677i -1.8439 - 1.3397i 0.7106 + 2.1870i -1.8604 - 1.3516i 0.7169 + 2.2063i -1.8768 - 1.3635i 0.7231 + 2.2254i -1.8930 - 1.3754i 0.7293 + 2.2444i -1.9092 - 1.3871i 0.7354 + 2.2633i -1.9253 - 1.3988i 0.7415 + 2.2822i -1.9413 - 1.4105i 0.7476 + 2.3009i -1.9572 - 1.4220i 0.7536 + 2.3195i -1.9731 - 1.4335i 0.7597 + 2.3380i -1.9888 - 1.4450i 0.7656 + 2.3564i -2.0045 - 1.4563i 0.7716 + 2.3747i -2.0201 - 1.4677i 0.7775 + 2.3930i -2.0356 - 1.4789i 0.7834 + 2.4111i -2.0510 - 1.4901i 0.7893 + 2.4291i -2.0664 - 1.5013i 0.7951 + 2.4471i -2.0816 - 1.5124i 0.8009 + 2.4650i -2.0968 - 1.5234i 0.8067 + 2.4828i -2.1120 - 1.5344i 0.8124 + 2.5004i -2.1270 - 1.5454i 0.8182 + 2.5181i -2.1420 - 1.5562i 0.8239 + 2.5356i -2.1569 - 1.5671i 0.8295 + 2.5530i -2.1718 - 1.5779i 0.8352 + 2.5704i -2.1865 - 1.5886i 0.8408 + 2.5877i -2.2012 - 1.5993i 0.8464 + 2.6049i -2.2159 - 1.6099i 0.8520 + 2.6221i -2.2305 - 1.6205i 0.8575 + 2.6391i -2.2450 - 1.6311i 0.8630 + 2.6561i -2.2594 - 1.6416i 0.8685 + 2.6731i -2.2738 - 1.6520i 0.8740 + 2.6899i -2.2882 - 1.6625i 0.8795 + 2.7067i -2.3025 - 1.6728i 0.8849 + 2.7234i -2.3167 - 1.6832i 0.8903 + 2.7401i -2.3308 - 1.6934i

More Answers (0)

Asked:

on 12 Apr 2022

Commented:

on 13 Apr 2022

Community Treasure Hunt

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

Start Hunting!