when converting array to table, it only shows double/sym
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
Please attach your whole code. A snippet of the code is not going to provide much information about the problem you are facing. If it is .m file, use the paperclip button to attach it, otherwise you can copy and paste it here as well.
Also, copy and paste the full error message i.e. all of the red text.
michael
on 21 Jul 2023
Dyuman Joshi
on 21 Jul 2023
Is using symbolic variables necessary to your code?
Also, you have not defined the variable z in your code.
michael
on 21 Jul 2023
Steven Lord
on 21 Jul 2023
Please show us the full and exact text of those warning and/or error messages (all the text displayed in orange and/or red in the Command Window) that you received. That information may be useful in determining what's going on and how to avoid the warning and/or error.
Dyuman Joshi
on 21 Jul 2023
Edited: Dyuman Joshi
on 21 Jul 2023
@michael, you still have not answered question - Is using symbolic variables neccessary to your code?
The reason I am asking is, because they can simply be avoided, so all the data is in double format throught out the code.
michael
on 21 Jul 2023
michael
on 21 Jul 2023
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)
michael
on 21 Jul 2023
Accepted Answer
More Answers (0)
Categories
Find more on Operations on Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!