Unable to perform assignment because the left and right sides have a different number of elements.

Hello,
I am fairly new to programming and MATLAB so this may be an easy solution. Basically I am trying to solve for a variable and call it "x0(3)". When I run it however, I get an error saying "Unable to perform assignment because the left and right sides have a different number of elements."
This is my code,
syms sy F d g2
lsf=(g2==sy*((pi*d^2)/4)-F);
mem=[61000,60000,1.5];
std=[5200,15000,0.021];
n=3;
for i=1:2
x0(i)=mem(i)
end
a=solve (lsf,d);
x0(3)=vpa(subs(a,[sy F d g2],[x0(1) x0(2) d 0]))
and my output,
When I change x0(3) to a letter, like B, then I get the value I am looking for. Is there a way I can make the variable set to x0(3)?
Thank you!

2 Comments

Jonathan - when you have something like
x0(3) = ...;
you are treating x0 as an array where you are setting the third element to a value. Do you mean for x0 to be an array? What are the dimensions of the output from vpa? I suspect that it isn't a scalar and that is why you are observing the error (and why it will work when you don't try to assign the output to an array).
Yes x0 is an array and I am trying to find the value for x0(3). After setting the variable to "B", I get:
syms sy F d g2
lsf=(g2==sy*((pi*d^2)/4)-F);
mem=[61000,60000,1.5];
std=[5200,15000,0.021];
n=3;
for i=1:2
x0(i)=mem(i)
end
a=solve (lsf,d);
B=vpa((subs(a,[sy F g2],[x0(1) x0(2) 0])))
Annotation 2019-06-14 133340.jpg
I used "vpa" simply because it gives me a decimal instead of a fraction. I don't beleive I need to have the negative B value shown above.

Sign in to comment.

Answers (1)

No loops needed:
syms sy F d g2
lsf=(g2==sy*((pi*d^2)/4)-F);
mem=[61000,60000,1.5];
std=[5200,15000,0.021];
n=3;
x0=mem(1:2);
a=solve(lsf,d);
b=vpa(subs(a,[sy F d g2],[x0(1) x0(2) d 0]));
Wanted=[repmat(x0,size(b)),b]

Asked:

on 14 Jun 2019

Commented:

on 14 Jun 2019

Community Treasure Hunt

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

Start Hunting!