when converting array to table, it only shows double/sym
24 views (last 30 days)
Show older comments
Hi good day, i'm creating a matlab program for simpson's 1/3 rule, all of the calculations are right but when i try to convert the arrays to table, it shows double or sym error, can you pls help me how to solve this?
here's the last part of the program
y=[front,both,end_];
arr=double(y) %simpson's 1/3 rule
arr2=double(z) %x_i
arr3=double(f_x) %f(x)
simpsons=[arr];
x_i=[arr2];
fx=[arr3];
tabulated_result=table(x_i,fx,simpsons);
format short
disp(tabulated_result)
10 Comments
Dyuman Joshi
on 21 Jul 2023
Just some suggestions - You can convert symbolic functions to function handles, and run the whole code through the same data format (i.e. numeric - double)
Converting data types multiple times should be avoided, as it saves both time and memory.
This code runs successfully on my computer (Can't run it here because Live Editor doesn't support input)
%%simpson.m
str = input('Give the equation in terms of x: ','s');
%Provide the input as
%exp(-0.5*x^2)/sqrt(2*pi)
%As exp() and pi are builtin function and constant in MATLAB respectively
%Convert the string to sum
fun = str2sym(str);
%Convert the symbolic function to a function handle
f = matlabFunction(fun);
a=-2; %input('enter the lower limit a: ');
b=1.6; %input('enter the upper limit b: ');
n=8; %input('enter the number of segments n: ');
h=(b-a)/n;
%Vectorize the for loop
i=1:n+1;
z=(a+(i-1)*h);
%A function handle (ideally defined for element-wise operations) can accept a vector as input
%and provides the output directly as a vector as well, eliminating the for loop
f_x = f(z);
front=f_x(1);
end_=f_x(end);
odd=(2*f_x(3:2:end-1));
even=(4*f_x(2:2:end-1));
summation=2*sum(odd)+4*sum(even)+sum(front)+sum(end_);
both(1:2:n)=even;
both(2:2:n-1)=odd;
y=[front,both,end_];
%Using [] for assinging variables is superfluous, better to avoid it
%as square brackets ([]) are concatenation operator
arr=y; %simpson's 1/3 rule
arr2=z; %x
arr3=f_x; %f(x)
simpsons=arr;
x_i=arr2;
fx=arr3;
%The data in Table is stored as columns, so transpose the variables
%to get column vectors
tabulated_result=table(x_i',fx',simpsons');
format short
disp(tabulated_result)
Accepted Answer
Cris LaPierre
on 21 Jul 2023
Edited: Cris LaPierre
on 21 Jul 2023
You are creating your table with row vectors. The entire vector cannot be displayed in the printout, so you are getting a description of the variable instead. I think you want to create column vectors instead.
- Specify the column in the assignment to z and f_x to be 1
- transpose y
syms x
str = '2.71828^(-0.5*x^2)/(sqrt(2*3.141591))';%input('Give the equation in terms of x: ','s') ;
f = function_handle.empty;
f = eval(['@()', str]);
f = f(); % Note the () is important
a=-2; %input('enter the lower limit a: ');
b=1.6; %input('enter the upper limit b: ');
n=8; %input('enter the number of segments n: ');
h=(b-a)/n;
for i=1:n+1
w=double(a+(i-1)*h);
z(i,1)=w;
f_x(i,1)=vpa(subs(f,x,w));
end
front=f_x(1);
end_=f_x(end);
odd=double(2*f_x(3:2:end-1));
even=double(4*f_x(2:2:end-1));
summation=2*sum(odd)+4*sum(even)+sum(front)+sum(end_);
both(1:2:n)=even;
both(2:2:n-1)=odd;
y=[front,both,end_]';
arr=double(y); %simpson's 1/3 rule
arr2=double(z); %x
arr3=double(f_x); %f(x)
simpsons=[arr];
x_i=[arr2];
fx=[arr3];
tabulated_result=table(x_i,fx,simpsons);
format short
disp(tabulated_result)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!