Conditional statements and loops
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti

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 Commenti
James Bowler
il 7 Ott 2021
Ravi Narasimhan
il 7 Ott 2021
Modificato: Ravi Narasimhan
il 7 Ott 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
il 7 Ott 2021
Ravi Narasimhan
il 7 Ott 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.
Risposte (0)
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
