Plot two functions with several changing variables

2 views (last 30 days)
Danny Brett
Danny Brett on 28 Dec 2021
Commented: the cyclist on 28 Dec 2021
I want to plot two functions against each other, however I currently have two changing variables (more to be added) however I get a couple error messages I cannot understand:
Matrix dimensions must agree.
Error in Footbridge_2 (line 9)
k = (p.*g + m.*g - n.*(m.*g) - (N-n).*m.*g) ./ -Lext;
Any help would be appreciated
p = 80;
m = 10;
n = 1:1:10;
g = 9.81;
N = 11;
Lext = 68:0.1:80;
L = 68;
k = (p.*g + m.*g - n.*(m.*g) - (N-n).*m.*g) ./ -Lext;
Arrays have incompatible sizes for this operation.
x = sqrt(Lext.^2 - L.^2);
plot(x,k)
xlabel('x')
ylabel('k')

Answers (3)

KSSV
KSSV on 28 Dec 2021
p = 80;
m = 10;
n = 1:1:10;
g = 9.81;
N = 11;
Lext = 68:0.1:80;
L = 68;
[n,Lext] = meshgrid(n,Lext) ;
k = p.*g + m.*g - n.*(m.*g) - (N-n).*(m.*g) ./ -Lext;
x = sqrt(Lext.^2 - L.^2);
plot(x,k)
xlabel('x')
ylabel('k')
  1 Comment
the cyclist
the cyclist on 28 Dec 2021
@KSSV, I ran your code revision, to show @Danny Brett what the output looks like.

Sign in to comment.


the cyclist
the cyclist on 28 Dec 2021
I ran your code inline here. I'm guessing the slightly different error message is due to different MATLAB versions.
The heart of your problem is that you are trying to divide the vector n by the vector Lext, and they have different lengths. A simplified version of what you are attempting is
x = [2 3 5];
y = [7 11];
z = x./y
Arrays have incompatible sizes for this operation.
What does it mean to divide the 3-element vector by the 2-element vector?
You need to figure out what you actually intended to do here.

VBBV
VBBV on 28 Dec 2021
p = 80;
m = 10;
n = 1:1:10;
g = 9.81;
N = 11;
Lext = linspace(68,80,length(n));
L = 68;
k = -(p.*g + m.*g - n.*(m.*g) - (N-n).*m.*g) ./ Lext;
x = sqrt(Lext.^2 - L.^2);
plot(x,k)
xlabel('x')
ylabel('k')
Make the vectors equal and plot it.
  1 Comment
the cyclist
the cyclist on 28 Dec 2021
FYI, @Danny Brett, this solution assumes that n and Lext are linearly related to each other, and not independent of each other. The code will execute, but that doesn't mean it does what you intend.

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by