Assignment between unlike types is not allowed. for loop

41 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 6 Nov 2018
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 Comments
Walter Roberson
Walter Roberson on 7 Nov 2018
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.)

Sign in to comment.

More Answers (1)

BHOLANATH KUMBHAKAR
BHOLANATH KUMBHAKAR on 9 Sep 2019
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 Comment
Walter Roberson
Walter Roberson on 9 Sep 2019
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)

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!