Switch conditions or formula in a continuous data

2 views (last 30 days)
Hello Expert, i'm chris and i'm really new in using MATLAB. I have a problem to make a continuous graphic with a different conditions. the condition is in a formula or maybe a function. first, i have two boundary conditions and the value of the boundary condition is calculated by a formula. then i have two boundary conditions, let's say it is X1 and X2. then for the data(for an example the data is the length of a beam) between 0 to X1 are the first condition. then for the data between X1 to X2 are the second condition and the data that is larger than X2 are the third conditions. and here is the script that i wrote.
A=input('A= ');
B=input('B= ');
C=input('C= ');
D=input('D= ');
E=input('E= ');
F=input('F= ');
G=input('G= ');
H=input('H= ');
I=input('I= ');
J=input('J= ');
%start calculation
X1=A+B;
X2=C+D;
L=1:1000; %it's the length data
y=zeros(size(L))
idx=L<X1;
y(idx) = E*F;
idx = L>=X1 & L<=X2
y(idx) = (G-H)*((L-X1)/(X2-X1));
idx = L>X2;
y(idx) = ((I/J)^2)*(H/L);
plot(L,y)
the script above didn't give me the conditions that i wanted. so, i need a help for solving this problem
Thank you in Advance

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 31 Dec 2011
try this is code
idx1=L<X1;
y(idx1) = E*F;
idx2 = L>=X1 & L<=X2;
y(idx2) = (G-H)*((L(idx2)-X1)/(X2-X1));
idx3 = L>X2;
y(idx3) = ((I/J)^2)*(H./L(idx3));
plot(L,y);
variant
[a,a] = histc(L,[-inf,X1,X2,inf]);
z = {@(x)E*F,@(x)(G-H)*((x-X1)/(X2-X1)),@(x)(I/J)^2*H./x};
out = arrayfun(@(x)z{x}(L(a==x)),1:3,'un',0);
y = [out{:}];
ADD [ 13:26(UTC+4) 01.01.2012]
Mn=zeros(size(Lb));
FZ = Fy*Zx;
Mn(Lb<=Lp)= FZ;
idx2=Lb>Lp & Lb<=Lr;
Mn(idx2)=Cb*(FZ - (FZ-Mr)*(Lb(idx2)-Lp)/(Lr-Lp));
idx3=Lb>Lr;
p1 = pi./Lb(idx3);
Mn(idx3)=Cb*p1.*sqrt(E*Iy*G*J+(E*p1).^2*Iy*Iw);
variant 2
Mn=zeros(size(Lb));
[a,a] = histc(Lb,[-inf,Lp,Lr,inf]);
FZ = Fy*Zx;
z = {@(x)FZ*ones(size(x)),...
@(x)Cb*(Fy*Zx-(FZ-Mr)*(x-Lp)/(Lr-Lp)),...
@(x)Cb*pi./x.*sqrt(E*Iy*G*J+(pi*E./x).^2*Iy*Iw)};
out = arrayfun(@(x)z{x}(Lb(a==x)),1:max(a),'un',0);
y = [out{:}];
  2 Comments
Chris Ryan
Chris Ryan on 1 Jan 2012
hello andrei,,thank you for the answer,,when i tried the first code it gave the error like this
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
and when i tried for the second one, it gave the error like this
??? Error using ==> mrdivide
Matrix dimensions must agree.
here it is, my actual code,the data x is Lb,and the output y is Mn. the rest of the input is already defined by the previous input.
Mn=zeros(size(Lb));
idx1=Lb<=Lp;
Mn(idx1)=Fy*Zx;
idx2=Lb>Lp & Lb<=Lr;
Mn(idx2)=Cb*((Fy*Zx)-(((Fy*Zx)-Mr)*((idx2-Lp)/(Lr-Lp))));
idx3=Lb>Lr;
Mn(idx3)=((Cb*pi)/idx3)*(sqrt((E*Iy*G*J)+(((pi*E)/idx3)^2)*Iy*Iw));
and the second one
Mn=zeros(size(Lb));
[a,a] = histc(Lb,[-inf,Lp,Lr,inf]);
z = {@(x)Fy*Zx,@(x)Cb*((Fy*Zx)-(((Fy*Zx)-Mr)*((x-Lp)/(Lr-Lp)))),@(x)((Cb*pi)/x)*(sqrt((E*Iy*G*J)+(((pi*E)/x)^2)*Iy*Iw))};
out = arrayfun(@(x)z{x}(Lb(a==x)),1:3,'un',0);
y = [out{:}];
what should i do with these kinds of error?
thank's again andrei,,
oh yeah,happy new year..
Andrei Bobrov
Andrei Bobrov on 1 Jan 2012
Hi Chris! Happy new year! See added with small corrected

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!