Assigned solve outputs to a matrix via for loops
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am having a problem where I am attempting to solve an equation inside of a for loop and assign its result to the respective matrix cell. It has inputs from a matrix of the same size as it should be outputting to, yet I still get the error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts". Here is my relevant code:
for i=1:s % i is steps of t
for j = 1:s % j is steps of r
Ac(i,j) = 2*pi*rp(j)*t(i);
if Ac(i,1)<An
Acrit(i,j) = Ac(i,1); % For distances less than .025mm
else
Acrit(i,j) = An; % For everything else
end
Aratio(i,j) = Ac(i,j)/Acrit(i,j);
end
end
for i=1:s
for j=1:s
M = sym('M');
macheqn(i,j) = 1/M*(2/(k+1)*(1+(k-1)/2*M^2))^((k+1)/(2*(k-1)));
assume(M,'real');
Mach(i,j)=double(solve(macheqn(i,j)==Aratio(i,j),M));
end
end
2 个评论
Darshan Ramakant Bhat
2017-3-28
What is 'An' is it a matrix or a constant? It would be better if you give input data as well
Walter Roberson
2017-3-28
Should we assume that k is a scalar?
Is there a reason to assign the same value to all elements of macheqn?
Your macheqn is not going to have an analytic solution so use vpasolve instead of solve.
Careful: the macheqn can have two solutions.
回答(2 个)
Nanda Gupta
2017-3-29
I understand that you want to solve an equation iteratively inside the for loop and assign its answer each time in to a respective matrix cell.
This error is a result of incorrect indexing of multidimensional arrays. Check out this documentation for indexing the multidimensional arrays: http://www.mathworks.com/help/matlab/math/multidimensional-arrays.html;jsessionid=b58f142b9ecaa292bc0adfa73247#f1-86846
0 个评论
Walter Roberson
2017-3-29
It looks likely to me that your solve() is returning an empty expression. double() of that is empty, and you would then be trying to store empty into a location of size 1.
We would need the values of the other variables to test this ourselves.
Try using vpasolve() instead of solve()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!