Using frprintf to display arrays having a string element
1 view (last 30 days)
Show older comments
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 Comments
dpb
on 26 Oct 2022
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.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!