fsolve and indexing

1 次查看(过去 30 天)
Aimee Flores
Aimee Flores 2012-5-21
I'm having a hard time trying to index my outputs into a vector because I wish to plot it. Here is my program:
R = 8.314;
for i = 1:4
MR(i) = i;
C_MeOH0(MR) = 1./(1 + MR);
C_MH0(MR) = MR./(1 + MR);
for j = 353:10:383
T(j) = j;
k1(j) = (6.74.*10.^8.*exp(-90000./(R.*j))).*3600;
k2(j) = (1.25.*10.^10.*exp(-105900./(R.*j))).*3600;
for k = 1:20
tau(k) = k;
F = @(C)[(1./(1 + i)) - C(1) - (k1.*C(1).*C(2).*k) - (2.*k2.*(C(1).^2).*C(2).*k); ((i./(1 + i)) - C(2) - (k1.*C(1).*C(2).*k) - (k2.*(C(1).^2).*C(2).*k))];
C0 = [1; 1];
C = fsolve(F, C0);
end
end
end
However when I try to index my C as C(i,j,k) I get this error:
??? Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in ==> try2 at 25 C(i,j,k) = fsolve(F, C0);
Can anyone please help me?

回答(2 个)

Walter Roberson
Walter Roberson 2012-5-21
fsolve() is returning multiple values. Which makes perfect sense considering that you have asked it to find the solution to a two-dimensional problem, so you expect to get back one value for each of the dimensions. You will want to write the output to an array with one more dimension than you have now.
(I recommend that you test to figure out what output you get if no solution is found.)
  1 个评论
Aimee Flores
Aimee Flores 2012-5-21
I'm not sure what you mean. Am I supposed to define some variable, let's call it 'a', which is some sort of expression so that my problematic line of code will be C(i,j,k,a) = fsolve(stuff) ? What is this expression I'm looking for? Is it a hypothetical output to the equations if no solution is found?

请先登录,再进行评论。


Sargondjani
Sargondjani 2012-5-21
i think you want to use cell arrays to store the vectors of the solutions:
C{i,j}=fsolve(....)
to call the content: C{i,j} will give you a 2 element vector you can regard this as a normal vector. to get the second element in the vector: C{i,j}(2)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by