Need help performing a Parametric Sweep...keep getting error

조회 수: 16 (최근 30일)
Joseph
Joseph 2016년 5월 4일
댓글: Star Strider 2016년 5월 5일
my code is below. I am trying to vary the constants r and K in the equation for M_spring. Then I want to plot M_spring vs R and M_spring vs K. i keep getting the error that matrix are not same dimensions. can someone assist me?? Thank you
%Input Given Parameters
%Lengths
l1=0.006;
l2=0.02;
l3=0.015;
l4=0.02;
l5=0.015;
wing = 0.033;
l = [l1 l2 l3 l4 l5];
L = .04;
%Masses
m1=0.00025;
m2=0.0005;
m3=0.00025;
m4=0.0025;
mass = [m1 m2 m3 m4];
%Pivots
d1x=0.015;
d1y=0.021;
d2x=-0.015;
d2y=0.021;
d = [d1x d1y d2x d2y];
%Inputs
th1dot=10;
th1=90
r =[0.05:.01:.2];
k = [0:5:500];
A_r = -2*d(1)*l(3) + 2*l(1)*l(3).*cos(th1);
B_r = -2*d(2)*l(3) + 2*l(1)*l(3).*sin(th1);
C_r = d(1)^2+d(2)^2+l(1)^2+l(3)^2-l(2)^2-...
2*l(1).*(d(2).*sin(th1)+d(1).*cos(th1));
th3 = 2*atan2(-B_r-sqrt(B_r.^2-C_r.^2+A_r.^2),C_r-A_r);
M_spring = ((d1x + r*sin(th3))/(d1y +r*cos(th3)))*k*L + m1*l1*cos(th1)*th1dot
plot(r,M_spring)
stay on
plot(k,M_spring)

채택된 답변

Star Strider
Star Strider 2016년 5월 5일
The easiest way to do this is to create an anonymous function from ‘M_spring’, create the appropriate matrices from ‘r’ and ‘k’ using the meshgrid function, and then evaluate it, then plot using subplot:
%Input Given Parameters
%Lengths
l1=0.006;
l2=0.02;
l3=0.015;
l4=0.02;
l5=0.015;
wing = 0.033;
l = [l1 l2 l3 l4 l5];
L = .04;
%Masses
m1=0.00025;
m2=0.0005;
m3=0.00025;
m4=0.0025;
mass = [m1 m2 m3 m4];
%Pivots
d1x=0.015;
d1y=0.021;
d2x=-0.015;
d2y=0.021;
d = [d1x d1y d2x d2y];
%Inputs
th1dot=10;
th1=90
r =[0.05:.01:.2];
k = [0:5:500];
[Mr,Mk] = meshgrid(r,k); % Create Matrices Of All Combinations Of (r,k)
A_r = -2*d(1)*l(3) + 2*l(1)*l(3).*cos(th1);
B_r = -2*d(2)*l(3) + 2*l(1)*l(3).*sin(th1);
C_r = d(1)^2+d(2)^2+l(1)^2+l(3)^2-l(2)^2-...
2*l(1).*(d(2).*sin(th1)+d(1).*cos(th1));
th3 = 2*atan2(-B_r-sqrt(B_r.^2-C_r.^2+A_r.^2),C_r-A_r);
M_spring_fcn = @(r,k) ((d1x + r.*sin(th3))./(d1y +r.*cos(th3))).*k.*L + m1.*l1.*cos(th1).*th1dot; % Create Anonymous Function From ‘M_spring’
M_spring = M_spring_fcn(Mr,Mk); % Calculate ‘M_spring’
figure(1)
subplot(2,1,1)
plot(r,M_spring)
xlabel('r')
ylabel('M_{spring}')
grid
subplot(2,1,2)
plot(k,M_spring)
xlabel('k')
ylabel('M_{spring}')
grid
The plot:
The upper plot gives a family of curves for various values of ‘k’ as a function of ‘r’, and the lower plot gives a family of curves for various values of ‘r’ as a function of ‘k’.
You probably could benefit by fewer ‘r’ values. A legend for each plot would also help. I have no idea what you’re doing, so I’ll let you sort these.
And May the Fourth be with you!
  댓글 수: 2
Joseph
Joseph 2016년 5월 5일
thank you. perfect
Star Strider
Star Strider 2016년 5월 5일
My pleasure!
Also consider a surf or mesh plot:
figure(2)
meshc(Mr, Mk, M_spring)
grid on
xlabel('r')
ylabel('k')
zlabel('M_{spring}')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by