Using frprintf to display arrays having a string element

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by