Assignment between unlike types is not allowed. for loop

Hello! I'm trying to solve a system of nonlinear equations,but my license doesn't support the fsolve function. The s1 is a parameter that takes values between 0 and 500, so i wanted to put that in a for loop and save the 3 values that i want(x=x1,s2=x2,D=x3) in arrays, in order to plot them.
m1=0.2;
m2=m1;
k1=15;
k2=25;
y1=0.4;
y2=0.3;
a1=0.05;
a2=5;
s1f=500;
s2f=600;
syms x1 x2 x3
eqns=[x3-(m1*s1)/(k1+s1+a2*x2)-(m2*x2)./(k2+x2+a1*s1)==0,x1-y1*(s1f-s1)+y2*(s2f-x2)==0,x3.*(s2f-x2)-((m2*x2)./(y2*(k2+x2+a1*s1))).*(y1*(s1f-s1)+y2*(s2f-x2))==0];
for s1=0:1:500
vars=[x1 x2 x3];
[sol1,sol2,sol3]=vpasolve(eqns,vars)
x(s1+1)=sol1;
s2(s1+1)=sol2;
D(s1+1)=sol3;
end
plot(x,D)
When i run the m.file i get:
roots2d
sol1 =
20.0
sol2 =
0
sol3 =
0
Assignment between unlike types is not allowed.
Error in roots2d (line 19)
x(s1+1)=sol1;
What am I missing here? Thank you in advance!

 Risposta accettata

You do not initialize x in this code. It probably has a value left over in the workspace.
It is recommended that you test whether you got a non-empty solution before you try to assign it into the array.
Your code uses s1 before defining it, and then tries to give it a changing value by using a "for" loop. You need to
syms s1
before defining the equations, and inside the loop you need
[sol1, sol2, sol3] = vpasolve( subs(eqns), vars);

3 Commenti

Before assigning the valyes to the matrix it seems i get back values like:
sol1 =
-10.915555199507655606016531036642
0.35879463612737391587568596621986
sol2 =
-20.385183998358852020055103455475
17.1959821204245797195856198874
sol3 =
-1.0259425583653950888694760943602
0.15201018191480203916654166416144
I set my x,s2 and D arrays=0 before the loop,as suggested, but when i try to assign the values to the variables i get the following:
syms s1
syms x1 x2 x3
eqns=[x3-(m1*s1)/(k1+s1+a2*x2)-(m2*x2)./(k2+x2+a1*s1)==0,x1-y1*(s1f-s1)+y2*(s2f-x2)==0,x3.*(s2f-x2)-((m2*x2)./(y2*(k2+x2+a1*s1))).*(y1*(s1f-s1)+y2*(s2f-x2))==0];
x=0;s2=0;D=0;
for s1=0:1:500
vars=[x1 x2 x3];
[sol1,sol2,sol3]=vpasolve(subs(eqns),vars)
x(s1+1)=sol1;
s2(s1+1)=sol2;
D(s1+1)=sol3;
end
roots
sol1 =
20.0
sol2 =
0
sol3 =
0
sol1 =
19.928724211652458257162389446416
18.363397895930432657392353616791
sol2 =
1.0957473721748608572079648213878
-4.1220070135652244753588212773629
sol3 =
0.017693374658243692671250695328618
-0.082775899986250910047041879353329
Unable to perform assignment because the left and right sides have a different number of elements.
Error in roots2d (line 20)
x(s1+1)=sol1;
For some reason it runs the for loop for 2 times before the error.
The case of s1 = 0 is degenerate and has only one solution. Otherwise the equations involve a quadratic with two solutions for each s1 value (at s1 = 0, the two are the same value.)
You were totally right,missed that. Thanks a lot!

Accedi per commentare.

Più risposte (1)

syms x al lm mu;
lm=3;
mu=5;
n=round(10/.01);
al=0:0.01:10;
for i=1:n+1
f(i)=((mu-lm*x)^((lm/al(i))-1))/(1-x)
end
z(i)=int(f,0,.99999)
--------------------------------------------------------------------------------
Assignment between unlike types is not allowed.
Error in Untitled3 (line 7)
f(i)=((mu-lm*x)^((lm/al(i))-1))/(1-x)
why is it say????can anyone help me???

1 Commento

I suspect that you have an existing variable named f that is interfering.
z(i)=int(f,0,.99999)
Caution, that is after the for loop and tries to integrate all of entries in the vector f with respect to the default variable, and assign the result to the single location z(i)

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by