Available under Creative Commons-ShareAlike 4.0 International License.
Given Area =, an analytical solution would produce 39. Use trapz command and solve it
- 1. Initialize variable x as a row vector, from 2 with increments of 0.1 to 5: x=2:.1:5;
- 2. Declare variable y as y=x^2;. Note the following error prompt : ??? Error using ==> mpower Inputs must be a scalar and a square matrix.
- This is because x is a vector quantity and MATLAB is expecting a scalar input for y. Because of that, we need to compute y as a vector and to do that we will use the dot operator as follows: y=x.^2;. This tells MATLAB to create vector y by taking each x value and raising its power to 2.
- 3. Now we can issue the following command to calculate the first area, the output will be as follows:
area1=trapz(x,y) area1 = 39.0050
Notice that this numerical value is slightly off. So let us increase the number of increments and calculate the area again:
x=2:.01:5; y=x.^2; area2=trapz(x,y) area2 = 39.0001
Yet another increase in the number of increments:
x=2:.001:5; y=x.^2; area3=trapz(x,y) area3 = 39.0000
- 瀏覽次數:1309