Regarding the vectors calculation

조회 수: 4 (최근 30일)
yogeshwari patel
yogeshwari patel 2024년 8월 31일
답변: Umar 2024년 8월 31일
x = 0:0.1:1;
t = 0.5;
u_exact = sin(x) .* exp(-t)
u_approx = sin(x) .* ((t^2 - 2*t + 2) / 2);
absolute_error = abs(u_exact - u_approx)
maximal_absolute_error = max(absolute_error)
fprintf('The maximal absolute error is: %f\n', maximal_absolute_error);
My query is u_exact is an array . How it is consider as array .
  댓글 수: 2
yogeshwari patel
yogeshwari patel 2024년 8월 31일
One more query why size of the array is 1 x 80 why it is not 1 X 11
DGM
DGM 2024년 8월 31일
u_exact is a vector because x is a vector.
Its size is 1x11.
x = 0:0.1:1;
t = 0.5;
u_exact = sin(x) .* exp(-t)
u_exact = 1x11
0 0.0606 0.1205 0.1792 0.2362 0.2908 0.3425 0.3907 0.4351 0.4751 0.5104
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size(u_exact)
ans = 1x2
1 11
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

답변 (1개)

Umar
Umar 2024년 8월 31일

Hi @yogeshwari patel ,

Please see my response to your comments below.

  • My query is u_exact is an array . How it is consider as array .*

In MATLAB, arrays can be either row or column vectors, or multidimensional matrices. In your code snippet:

x = 0:0.1:1;  
t = 0.5;
u_exact = sin(x) .* exp(-t);

The variable x is defined as a row vector ranging from 0 to 1 with increments of 0.1 which results in 11 elements.

x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]`.

If you notice in my attached file, the expression sin(x) .* exp(-t) computes the sine of each element in x and multiplies it by exp(-t), which is a scalar value (approximately 0.6065). The result is also a row vector because MATLAB performs element-wise operations on arrays of compatible sizes.

Therefore, u_exact becomes:

u_exact = [0, 0.0606, 0.1205, 0.1792, 0.2362, 0.2908, 0.3425, 0.3907, 0.4351, 
0.4751, 0.5104], which has a size of  1x11 (one row and eleven columns).
  • One more query why size of the array is 1 x 80 why it is not 1 X 11*

Regarding your query about the size of the array being reported as 1x80, it appears there may have been a misunderstanding or miscommunication: When you execute the command `fprintf('The size of u_exact is: %dx%d\n', size(u_exact)); , it should correctly output:

 The size of u_exact is: 1x11 (this is what @DGM trying to tell you)

For more information on function “size”, please refer to

https://www.mathworks.com/help/matlab/ref/size.html

Now, in your case, since you mentioned seeing a size of 1x80, it could indicate that either the variable has been inadvertently modified somewhere else in your code to include more elements and not being shared with us in your posted comments or you performed a previous computation that expanded u_exact into a larger array.

Please see attached.

Hope this answers your question, please let us know if you have any further questions.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by