Conditional statements and loops

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 件のコメント

James Bowler
James Bowler 2021 年 10 月 7 日
Been trying to use conditional statements and loop commands to do this question. Any help is appreciated.
Ravi Narasimhan
Ravi Narasimhan 2021 年 10 月 7 日
編集済み: Ravi Narasimhan 2021 年 10 月 7 日
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
You can define an anonymous function in Matlab:
y = @(A,B,C,D,x) (A*x.^2/B).*(C*x.^3-D*x) % Note .* and .^ vs plain *s
y = function_handle with value:
@(A,B,C,D,x)(A*x.^2/B).*(C*x.^3-D*x)
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
James Bowler 2021 年 10 月 7 日
Thanks for the answer. Yes it is required to use conditional statements and loops
Ravi Narasimhan
Ravi Narasimhan 2021 年 10 月 7 日
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.

回答 (0 件)

この質問は閉じられています。

質問済み:

2021 年 10 月 7 日

閉鎖済み:

2021 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by