Conditional statements and loops
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen

Write a program that plots the deflection of the beam y as a function of x. First create a 100 element vector for x, then by using the LOOP and CONDITIONAL STATEMENTS calculate valute of y for the corresponding value of x.
4 Kommentare
James Bowler
am 7 Okt. 2021
Ravi Narasimhan
am 7 Okt. 2021
Bearbeitet: Ravi Narasimhan
am 7 Okt. 2021
I don't know if you are required to use loops and/or conditionals. If not, you might consider adapting the following approach.
Suppose
assuming B is not equal to 0. Your equations are more complicated but this should illustrate
y = @(A,B,C,D,x) (A*x.^2/B).*(C*x.^3-D*x) % Note .* and .^ vs plain *s
Initialize some values
A = 1; B = 2; C = 3; D = 4;
x = linspace(0,10,100); % equally spaced pts between 0 and 10
Pass the arguments and linearly spaced array of x points to the anonymous function and get the result in one shot. No loop needed.
result = y(A,B,C,D,x);
plot(x,result)
You could create anonymous functions for each of the two regions and pass each a linearly spaced array to get your answers.
James Bowler
am 7 Okt. 2021
Ravi Narasimhan
am 7 Okt. 2021
Then, you can
1) Define your constants
2) Define an x array of some appropriate length in terms of L. e.g. x = linspace(0,L,50);
3) Define an empty y array. y= [];
4) Loop over the elements of the x array
- If the current element is less than L/2,
- evaluate your first equation and append the result to y: y = [y, evaluated function value]
- else
- If the current element is greater than L/2,
- evaluate your second equation and append the result to y
5) End the loop
This is not optimal or efficient but it satisifies the requirements.
Antworten (0)
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
