Using frprintf to display arrays having a string element

1 回表示 (過去 30 日間)
Ali Kiral
Ali Kiral 2022 年 10 月 26 日
コメント済み: dpb 2022 年 10 月 26 日
The script..
taylor(1)=-0.4;taylor(2)=-0.3786666667; epsilon=0.5*10^(-6); i=1;
while abs ((taylor(i+1)-taylor(i))/taylor(i+1))>=epsilon
taylor(i+2)=taylor(i+1)+((-1)^(i+1)/(2*(i+1)+1))*(-0.4)^(2*(i+1)+1);
error(i)=abs ((taylor(i+1)-taylor(i))/taylor(i+1))*100;
i=i+1;
end
for j=1:i+1
true_values(j)=atan(-0.4);
end
for k=1:i+1
true_error(k)=(true_values(k)-taylor(k))/true_values(k)*100;
end
number_of_approximations=1:i+1;
fprintf(' Number of terms Approximations True value True percent relative error\n')
Results=[number_of_approximations;taylor;true_values;true_error];
fprintf('%10.0f %28.16f %22.16f %30.16f\n',Results)
..displays 4 8x1 arrays. I want to concatenate the array 'error' along with those 4 arrays but it has 6 elements. With it not having 8 elements, MATLAB returns 'Error using vertcat, dimensions of matrices being concatenated are not consistent' That is fine.
Now i want to append a string 'undefined' to the first index of array 'error' and some number to the end of it, thus creating an 8 element array, then print it along with 'Results'. I used num2cell, but fprintf does not accept cells as input.
What do you suggest?
  3 件のコメント
Ali Kiral
Ali Kiral 2022 年 10 月 26 日
Ok, I will change the array name. 'Error' consists of approximate errors each of which requires at least two estimations( (Previous estimation-Current estimation / Cuurent estimation)). I cannot have an approximate error when I only have had one estimation (i.e. the first guess) The other missing element is the last one. When the stopping criterion is met i give up calculating that error. Thus' error' is two element short of other arrays
This script is actually about the Taylor series of arctan(-0.4) and stopping adding terms when a certain criterion is met
dpb
dpb 2022 年 10 月 26 日
@Image Analyst specifically means to not use error as a variable...
true_values(j)=atan(-0.4)
Since the true value is a constant, you don't need an array of the same value, just
true_value=atan(-0.4);
will do it and then you can compute the difference also without a loop so it will always have the same number of elements as does the length of the computed series.
As a hint about what @Image Analyst asks on the lengths, why as present the number is different has to do with the indexing expressions you're using to compute the new taylor term as compared to the value of i in the loop as well as then which terms you're using to compute the convergence error term -- they're all different; think about what that means.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by