You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
kindly help me correct my code. I want the s output to be same length as the output
1 view (last 30 days)
Show older comments
f=60 ;
gg=tan(f);
H=6;
l=[-6:0.1:20];
x=l'
if (l<0)
s=0
end
if (l>=0&l<(H/gg))
s=l.*gg
end
if (l>=(H/gg))
s=H
end
cc=length(x)
ll=length(s)
plot(l,s,'r-')
Hello. I want to write this code such that the length of x and s are thesame so I can use it to draw lines. I have tried a couple of options but can't seem to figure it out, I only get one output of s. can anybody help me out. thank you.
Accepted Answer
Star Strider
on 28 Mar 2016
I suspect you mean the output to be the same length as the input. The easiest way is to use logical indexing, essentially adding two vectors that have the defined values over the ranges you want them to, and zero elsewhere. This produces the output vector you want.
This works:
l=[-6:0.1:20];
f=60 ;
gg=tan(f);
H=6;
sfcn = @(l,f,gg,h) [((l>=0) & (l<(H/gg))).*(l*gg) + (l>=(H/gg)).*h];
s = sfcn(l,f,gg,H);
figure(1)
plot(l, s, '-r')
35 Comments
Brendan Atarigiya
on 28 Mar 2016
hi Star Strider. this answers my question perfectly. I will however be glad if you could explain it to me, especially variables sfcn and s. thank you very much.
Star Strider
on 28 Mar 2016
My pleasure.
The essential logic of the code takes advantage of the fact that in MATLAB, logical vectors are set to a logical value of 1 if a condition is ‘true’, and 0 if the condition is ‘false’. These logical values automatically turn into their decimal equivalents (+1,0) when they are used in calculations.
The easiest way to explain the logic in the function is to take one section of the code as an example:
(l>=(H/gg)).*h
This part of the code creates a ‘logical index’ vector the length of ‘l’ (so an equivalent length to the input) that is 0 everywhere except where (l>=(H/gg)). In that region, since it is ‘true’ there, the value of the logical vector is 1, that part of the vector is set to +1, and multiplies it by ‘h’.
The function used in conjunction with a logical test can be a function of the vector itself, as it is in the first condition. The same logic and calculations apply.
So what the function does in my code here is to create two logical vectors that are of length ‘l’, sets them to ‘true’ where the appropriate logic test for each applies, multiplies the the appropriate vector by the appropriate function for each vector, and adds the vectors element-wise to produce the output.
The ‘sfcn’ anonymous function is the function that applies this logic to its arguments. I called it that because it is the function that creates your output vector ‘s’. See the documentation on Function Basics for details.
You called your output ‘s’, so I did the same in my code to avoid confusion.
Brendan Atarigiya
on 29 Mar 2016
thank you very much for your time. I appreciate and will keep in touch.
Brendan Atarigiya
on 1 Apr 2016
hello Star Strider, l=linspace(1,20,50). please can you help me discretize the above array such that I can always refer to l(1)....l(n) in an equation? thank you.
Star Strider
on 1 Apr 2016
My pleasure.
You can do that with the vector linspace creates. The difference between the colon operator and the linspace function is that the colon operator has a defined step size that produces a variable-length vector. The linspace function varies the step size to produce a defined-length vector.
So with the colon operator, you choose the step size, and with linspace you choose the vector length.
Here, ‘s’ will always be the same length ‘l’ is, so you would refer to both as you would refer to any other vector.
Brendan Atarigiya
on 1 Apr 2016
Edited: Brendan Atarigiya
on 1 Apr 2016
assuming I need the 10 th sept for as an input, how do I refer to the 10th step in the vector? thank you.
Star Strider
on 1 Apr 2016
You would do that the same way you would reference any other vector:
l(10)
and correspondingly,
s(10)
for the value of ‘s’ at ‘l(10)’.
Brendan Atarigiya
on 2 Apr 2016
Edited: Star Strider
on 2 Apr 2016
thank you for the response. it works just as you said it. I however want to write something like this:
for i= linspace(-3.1803,22.1421,10);
b(i)=(b(i+1))+(b(i));
end
this will help me determine the width of at every i th step. thank you again.
Star Strider
on 2 Apr 2016
My pleasure.
You can’t do that. Subscripts must be positive integers or logicals, so your ‘b’ assignment will fail in that context. (I’m not certain where ‘b’ came from anyway. It was not predefined in the code you posted, so that would throw an error as well.)
If you want to determine the width of every step linspace calculates, just do:
i= linspace(-3.1803,22.1421,10);
istep = diff(i);
with ‘istep’ giving you the information you want, although being one element shorter in length than ‘i’.
They should all be approximately equal, with allowances for floating point conversion inaccuracies. Taking the mean and std of ‘istep’ would convince you, since std(istep) would be very low.
Brendan Atarigiya
on 2 Apr 2016
Edited: per isakson
on 2 Apr 2016
hello Star Strider,
I tried your recommendation and it worked for b. Kindly run this code. It will give you a preview of what I have been trying to do and advise help me correct the wrong parts. This is not the end of the code but I am hoping I can be able to get far when you guide me through this stage. thank you for your time. I really appreciate it.
%%%this code calculates the factor of safety of a homogeneous soil mass
%%%using the method of slices and the M-P interslice force function
close all
clear variables
clc
% axis([-10 50 -30 30])
%%slope and soil parameters
a=8; %x coordinate of center of circle
b=10; %y coordinate of center of circle
r=15; %radius of center of slip circle
f=30 ; %slope angle in degrees
G=18; %unit weigh of soil
c=28; %cohesion of soil
phi=30; %frictional angle of soil
gg=tan(f*pi/180); %slope angle in gadient
H=5; %height of slope
%%slip surface generation
xmin = a - sqrt((abs(r^2 - b^2))) %exit point of slip circle
xmax = a + sqrt((abs((r^2) - ((b-H)^2)))) %entry point of slip circle
l = linspace(xmin,xmax,5);
gr = length(l); %length of l: xmax - -xmin
q=b-sqrt(abs(r^2)-((l-a).^2));% equation for generating slip circle
yb = q' %slip surface
hold on
plot(l,yb,'r.')
%%slope surface generation
sfcn = @(l,f,gg,h) [((l>=0) & (l<(H/gg))).*(l*gg) + (l>=(H/gg)).*h]; %equations for generating slope surface. equation of a line is used
s = sfcn(l,f,gg,H);
yt=s'; %slope surface
figure(1)
plot(l, yt, '.r')
hold off
ss=length(yt);
%
i= l;
bb = diff(i);
b=bb'
%
j=yb;
yb1=diff(j);
yb2=abs(yb1);
%
sa=atan(yb2(i)/b); %slice angle. the angle is negative before yb(3) and positive after yb(3)
ll(i)=b/cos(sa(i));
%
h(i)=((yt(i)-yb(i))+(yt(ii)-yb(ii)))/2 %height of slope
W(i)=h(i)*b*20
Brendan Atarigiya
on 2 Apr 2016
Edited: Brendan Atarigiya
on 2 Apr 2016
sorry the first one was messed up.
%%%this code calculates the factor of safety of a homogeneous soil mass
%%%using the method of slices and the M-P interslice force function close all
clear variables
clc
% axis([-10 50 -30 30])
%%slope and soil parameters
a=8; %x coordinate of center of circle
b=10; %y coordinate of center of circle
r=15; %radius of center of slip circle
f=30 ; %slope angle in degrees
G=18; %unit weigh of soil
c=28; %cohesion of soil
phi=30; %frictional angle of soil
gg=tan(f*pi/180); %slope angle in gradient
H=5; %height of slope
%%slip surface generation
xmin = a - sqrt((abs(r^2 - b^2))) %exit point of slip circle
xmax = a + sqrt((abs((r^2) - ((b-H)^2)))) %entry point of slip circle
l = linspace(xmin,xmax,5);
gr = length(l); %length of l: xmax - -xmin
q=b-sqrt(abs(r^2)-((l-a).^2));% equation for generating slip circle yb = q' %slip surface
hold on plot(l,yb,'r.')
%%slope surface generation
sfcn = @(l,f,gg,h) [((l>=0) & (l<(H/gg))).*(l*gg) + (l>=(H/gg)).*h]; %equations for generating slope surface. equation of a line is used
s = sfcn(l,f,gg,H);
yt=s'; %slope surface
figure(1)
plot(l, yt, '.r')
hold off
ss=length(yt);
i= l;
bb = diff(i);
b=bb'
j=yb;
yb1=diff(j);
yb2=abs(yb1);
sa=atan(yb2(i)/b); %slice angle. the angle is negative before yb(3) and positive after yb(3) ll(i)=b/cos(sa(i));
%
h(i)=((yt(i)-yb(i))+(yt(ii)-yb(ii)))/2 %height of slope W(i)=h(i)*b*20
Star Strider
on 2 Apr 2016
I can’t read that.
Please format your code. Highlight it, then click on the [{}Code] button at the top of the text input window. Used the ‘Edit’ (pencil icon) to open the Comment window up for editing, then the ‘Submit’ button below the window to save your changes.
Star Strider
on 2 Apr 2016
In this line:
yt=s'; %slope surface
you have not defined ‘s’. Do you want to calculate it from ‘sfcn’ or is it data you imported earlier in your code? (I have no way of knowing.)
Brendan Atarigiya
on 2 Apr 2016
hello. I have made the editing. I want to calculate it from 'sfcn'. Thank you
Brendan Atarigiya
on 2 Apr 2016
I want to refer to the slip surface verctor which I have given variable name 'yb'. I can not figure out why it says it's undefined variable.
Star Strider
on 2 Apr 2016
When I just looked, that line was buried in the comment after the ‘q’ assignment. It wasn’t a separate line:
q=b-sqrt(abs(r^2)-((l-a).^2));% equation for generating slip circle yb = q' %slip surface
I didn’t look in the comments for it.
This line is easy enough to vectorise:
sa=atan(yb2./b);
but I have no idea what you are doing here, because you have not defined ‘ii’:
h(i)=((yt(i)-yb(i))+(yt(ii)-yb(ii)))/2 %height of slope W(i)=h(i)*b*20
Also, you defined i=l, and ‘l’ is a (1x5) non-integer vector. You cannot use it as a subscript vector. Subscripts can only be positive integers (or logical vectors).
Brendan Atarigiya
on 3 Apr 2016
In this line below, I want to find the average height of a slice. this is done by find the average of the heights of two strips. maybe the attachment can make you see what I am saying better. I want to find the height along W or Xci in the images. t thank you
(h(i)=((yt(i)-yb(i))+(yt(ii)-yb(ii)))/2 %height of slice
W(i)=h(i)*b*20
Star Strider
on 3 Apr 2016
The attachment will probably help. I went back through the entire thread and can’t find it.
Use the ‘paperclip’ icon, and then complete both the ‘Choose file’ and ‘Attach file’ steps.
Star Strider
on 3 Apr 2016
Well, the maths are straightforward, but your code is not. I have no background in civil engineering or soil mechanics, so I can’t determine where the problem may be in the logic in your code.
It seems to me that:
h = (yt-yb)/2
would be what you want, but I have no idea.
Brendan Atarigiya
on 5 Apr 2016
This will give me the height at the midpoint of the slice. If you do not mind, I will send you my algorithm and the work so far so we you can understand what I want to write.
Star Strider
on 5 Apr 2016
This seems to be a problem with coding what you want to do. The purpose of MATLAB Answers is to address specific problems with code you have already written.
Please understand that civil engineering and soil mechanics are not areas of my expertise, or something I particularly want to learn. I suggest you take this up with your professor or someone else skilled in soil mechanics who understand and have likely encountered this problem before, and can help you code it.
Star Strider
on 5 Apr 2016
My pleasure.
My background is in biomedical signal processing, control theory, and some related technical areas. I might be able to help you code your problem if it was in an area of my expertise, but not with a problem so far removed from them.
Brendan Atarigiya
on 7 Apr 2016
hello Star Strider, Can you please help me write this code for all 'ht'. Thank you.
ytop=[0 0 2.4 5.0 5.0 5.0 5.0 5.0 5.0 5.0]
ybot=[0 -2.4 -3.9 -4.7 -4.9 -4.7 -3.8 -2.3 0.1 5.0]
ht=((ytop(2)-ybot(2))+(ytop(1)-ybot(1)))/2
Ilham Hardy
on 7 Apr 2016
for id = 1:length(ytop)-1
htloop(id) = ((ytop(id+1)-ybot(id+1))+(ytop(id)-ybot(id)))/2;
end
Was wondering how to do this without the for loop...
Star Strider
on 7 Apr 2016
Without the loop:
htloop = (((ytop(2:end)+ytop(1:end-1))-(ybot(2:end)+ybot(1:end-1))))/2;
GMT-6 here.
Brendan Atarigiya
on 23 Jun 2016
hello Star Strider. I am here again with a problem. I want to write a loop to behave like I have below. hope you are doing good? thank you
guess initial y for l=0:0.2:1 y1=x*l +w/y end new_y=y1
More Answers (0)
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)