Regarding the vectors calculation

2 次查看(过去 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