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

16 次查看(过去 30 天)
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 个评论
Dyuman Joshi
Dyuman Joshi 2023-7-21
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)

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2023-7-21
编辑:Cris LaPierre 2023-7-21
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 个评论
michael
michael 2023-7-21
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'})

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by