Having issues with fprintf statement. It gives me "error using vertcat"

5 views (last 30 days)
clear
clc
a = 1.5;
b = 4.2;%lower and upper bounds
f = @(x) 2*x.^5 -x.^4 + 0.7*x.^3 - 2*x + 13;
I_exact = integral (f,a,b)
I_exact = 1.6393e+03
MPRTE = @ (est) abs ((I_exact -est)/I_exact)*100;
MPRAE = @(current, previous) abs((current-previous)/current)*100;
%first iteration
I11 = trap(f,a,b,1);
I12 = trap(f,a,b,2);
I21 = 4/3 * I12 - 1/3* I11;
MPRAE_I21 = MPRAE(I21,I12);
%2nd iteration
I13 = trap(f,a,b,4);
I22 = 4/3*I13 - 1/3*I12;
I31 = 16/15 *I22-1/15*I21;
MPRAE_I31 = MPRAE (I31, I22);
%3rd iteration
I14 = trap(f,a,b,8);
I23 = 4/3* I14 - 1/3*I13;
I32 = 16/15 * I23 - 1/15*I22;
I41 = 64/63 * I32 - 1/63 * I31;
MPRAE_I41 = MPRAE(I41,I32);
fprintf(['j \t k =1 \t k=2 \t k=3 \t k=4 \n' ...
'- ---------- ---------- ---------------\n',
'1 %4.5f %-4.5f %4.5f %4.10f\n', ...
'2 %4.5f %-4.5f %4.5f\n ', ...
'3 %4.5f %-4.5f\n', ...
'4 %4.5f\n'], I11,I12,I13,I14,I21,I22,I23, I31,I32,I41)
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
function I = trap(func,a,b,n,varargin)
% trap: composite trapezoidal rule quadrature
% I = trap(function,a,b,n,p1,p2,...):
% composite trapezoidal rule
%
% Inputs:
% func = function to be integrated
% a,b = integration limits
% n = number of segments (default = 100)
% p1, p2,... = additional parameters used by func
% Output:
% integral estimate
%Created by: Isheeta Ranade
% Feb 08, 2017
% Updated on Feb 13, 2018
if nargin<3, error('At least 3 input arguments required'), end %Error check to ensure 3 inputs are included
if ~(b>a), error('Upper bound must be greater than lower'), end % Ensure that b is greater than a. If not exit the function
if nargin<4 || isempty(n), n = 100; end %Ensure n is specified. If not set to 100
x = a;
h = (b-a)/n; %Compute step size h
s = func(a,varargin{:});
for i = 1:n-1
x = x+h; %Increment location of x
s = s + 2*func(x,varargin{:}); %Include the summation of all interior segments into s
end
s = s + func(b,varargin{:}); %Add the last term to s
I = (b-a)*s/(2*n); %Compute the integral approximation using the summation term.
end

Accepted Answer

Simon Chan
Simon Chan on 2 Oct 2022
Missing dots in the second line, try the following
fprintf(['j \t k =1 \t k=2 \t k=3 \t k=4 \n'...
'- ---------- ---------- ---------------\n',... % <--- Missing dots
'1 %4.5f %-4.5f %4.5f %4.10f\n', ...
'2 %4.5f %-4.5f %4.5f\n ', ...
'3 %4.5f %-4.5f\n', ...
'4 %4.5f\n'], I11,I12,I13,I14,I21,I22,I23, I31,I32,I41)

More Answers (0)

Categories

Find more on Source Control Integration in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by