Filter löschen
Filter löschen

fsolve and indexing

2 Ansichten (letzte 30 Tage)
Aimee Flores
Aimee Flores am 21 Mai 2012
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?

Antworten (2)

Walter Roberson
Walter Roberson am 21 Mai 2012
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 Kommentar
Aimee Flores
Aimee Flores am 21 Mai 2012
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?

Melden Sie sich an, um zu kommentieren.


Sargondjani
Sargondjani am 21 Mai 2012
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)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by