when converting array to table, it only shows double/sym

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

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.
hi! thanks for responding, the pic is the output, here's the file for the whole program
Is using symbolic variables necessary to your code?
Also, you have not defined the variable z in your code.
the z is defined at the for loop, everything is right except for the display when i converted the arrays to table, the table only shows "double" but the data inside the arrays are already correct
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.
@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.
sym is neccessary since the user needs to input the equation, i found a way how to do it, instead of naming the array (x_i=[arr]) i use the command VariableNames, and it works now, thanks for the time! i appreciate it @Dyuman Joshi
@Steven Lord there is no error, instead of naming the array i use the VariableNames command and it works now, thanks for responding
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)
@Dyuman Joshi thanks again! i'll put into use those insights

Sign in to comment.

 Accepted Answer

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)
x_i fx simpsons _____ ________ ________ -2 0.053991 0.053991 -1.55 0.12001 0.48004 -1.1 0.21785 0.4357 -0.65 0.32297 1.2919 -0.2 0.39104 0.78209 0.25 0.38667 1.5467 0.7 0.31225 0.62451 1.15 0.20594 0.82375 1.6 0.11092 0.11092

1 Comment

thanks for this! so this what i'm missing, i'm able to solve it doing this
tabulated_result=table(arr2',arr3',arr','VariableNames',{'x_i','fx_i','Simpsons_rule'})

Sign in to comment.

More Answers (0)

Tags

Asked:

on 21 Jul 2023

Commented:

on 21 Jul 2023

Community Treasure Hunt

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

Start Hunting!