problem about the scipt

clear all
l = 25
E = 200*10^9
I = 350*10^-6
w = 6*10^3
x = input('enter length of the beam')
if (x>=0)&(x<= l/2),
y = -w*x/384*E*I *(16*x^3-24*l*x^2+9*l^3);
elseif
(x>=l/2)& (x<= l);
y = -w*x/384*E*I*(8*x^3-24*l*x^2+(17*l^2)*x-l^3)
end
Why I can not get the value of y? How to get it?

10 Comments

Do you get any error from the compiler? I have run the code and it works on my computer
What value of y are you expecting? What does this code do? Note that you seem to be conditioning on whether or not x is within a certain range: between 0 and 25/2 or between 25/2 and 25 (where l=25). So y won't be set if your input is greater than 25.
Your else statement is not quite correct. The condition should be:
elseif (x>=l/2) && (x<= l)
rather than just the else with a condition that is not "attached" to anything.
Typically the double ampersand is used in conditions. See short-circuit for details.
There is no any error from the compiler. How can I define the x between 0 and l to get the value of y?
The y variable is always being set in your above code which may or may not be correct given what appears to be a condition in the else block when in fact it the condition should be part of an elseif statement. (This is noted in the above comment and in the answer from Image Analyst.) Could you verify this condition and then correct your above code?
As for defining x you have set l to be 25. So that means that as long as the input value (which is x) is between 0 and 25, then the y will be set to something. If y is being incorrectly set, then the code (or equation for y) has to be changed by you.
Shi Yuhao
Shi Yuhao on 28 Apr 2014
Edited: Shi Yuhao on 28 Apr 2014
I have already change the statement, but it makes no sense.It still can not appears the y.
What is your value of x? What is the resulting y?
x is part of l, 0=<x<=l, y is deflection of the beam
What do the following variables represent, and please include the units for each: l, E, I, w and y? I realize that y is the deflection of the beam but what are the units for it?
Is E the modulus of elasticity? Is I the area moment of inertia?
Based on some notes at deflection, your product of E*I should be in the denominator and not in the numerator. i.e. instead of
-w*x/384*E*I
should this be
-w*x/(384*E*I)
What are the equations that you are trying to translate into MATLAB code?
Is the real problem that you are not getting the y that you expect?
E is the modulus of elasticity. I is the area moment of inertia. l is the length of the beam. w is the constant distributed load. The equation is provided by the question. It asks me to use the loop to do the calculation. But I haven't got result of it.
What are you asked to loop over? There is no loop in your above code. As well, please verify your equation. Perhaps post it here and we can help make sure that you have coded it up (in MATLAB) correctly.

Sign in to comment.

 Accepted Answer

y DOES get assigned. Verify by stepping through your code. If you don't know how to do that, see this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ Also, the line that says
(x>=l/2)& (x<= l);
should probably have a % in front of it because it doesn't do anything and appears like it should be a comment.

5 Comments

Shi, it DOES get assigned. It must either go into the if or into the else. It won't just skip them both. Here, I made some slight changes. Try this:
l = 25
E = 200*10^9
I = 350*10^-6
w = 6*10^3
x = input('enter length of the beam : ')
if (x>=0) && (x<= l/2)
y = -w*x/384*E*I *(16*x^3-24*l*x^2+9*l^3)
else
y = -w*x/384*E*I*(8*x^3-24*l*x^2+(17*l^2)*x-l^3)
end
It doesn't work.
Your short answer makes us want to stop helping you. The fact is, it does work as anyone (except you) who copies and pastes this code and steps through it can easily verify. The y does indeed definitely definitely definitely does get assigned. Geoff, pietro, and I all say so.
Why don't you run PSR.exe (if you're using Windows) and show us, screenshot by screenshot, you stepping through this code, and prove to us that it never steps on the y line at all?
I am sorry to write such a answer. I am new to the matlab and I am confused about it. The results seems it never stops. Maybe I misunderstand the question. The question asks me to do the calculation using loop. The equation is provided by the question. It gives the value of w,E,I,and l.
I'm really baffled as to how that can be true. Please start a new script and paste this code in there exactly . I mean exactly - nothing else but this code below:
clc; % Clear the command window.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fprintf('Starting code...\n');
l = 25
E = 200*10^9
I = 350*10^-6
w = 6*10^3
x = input('enter length of the beam : ')
if (x>=0) && (x<= l/2)
fprintf(' Now entering the "if" block of code to set the first y...\n');
y = -w*x/384*E*I *(16*x^3-24*l*x^2+9*l^3)
else
fprintf(' Now entering the "if" block of code to set the first y...\n');
y = -w*x/384*E*I*(8*x^3-24*l*x^2+(17*l^2)*x-l^3)
end
fprintf('Code has ended!\n');
Now, copy and paste what you see in your command window. When I type in 0.5, I see the following:
Starting code...
l =
25
E =
200000000000
I =
0.00035
w =
6000
enter length of the beam : 0.5
x =
0.5
Now entering the "if" block of code to set the first y...
y =
-76823359375000
Code has ended!
What do you see in your command window? Anything? What does "doesn't stop" mean? Does it doe it over and over again forever? Does it just do absolutely nothing at all with nothing in the command window whatsoever? What do you see?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!