How to integrate a function using a GUI
5 次查看(过去 30 天)
显示 更早的评论
Hi. I'm using a GUI to solve a multiple integral, but the code that i'm using doesn't work :(
I'm using MATLAB 2016
syms x y z;
Funcion=get(handles.EtxtFuncion,'String');
A1=get(handles.EtxtA1,'String');
B1=get(handles.EtxtB1,'String');
A2=get(handles.EtxtA2,'String');
B2=get(handles.EtxtB2,'String');
A3=get(handles.EtxtA3,'String');
B3=get(handles.EtxtB3,'String');
D1=get(handles.EtxtD1,'String');
D2=get(handles.EtxtD2,'String');
D3=get(handles.EtxtD3,'String');
I1=integral(Funcion,D1,A1,B1);
I2=integral(I1,D2,A2,B2);
I3=integral(I2,D3,A3,B3);
set(handles.ResTotal,'String',num2str(I3));
I really appreciate your help :)
0 个评论
回答(1 个)
Walter Roberson
2020-6-6
When you get the String property of an object, the result is a character vector (or sometimes a cell array of character vectors, depending on the object.)
You are trying to use integral() with that character vector. However, integral() is reserved for doing numeric integration, and the first parameter you pass to integral() must be a function handle.
We could suggest that you use str2func() to convert the character vector into something you could pass to integral(), but we see that you integral() the result of integral(). That tells us that what you expect out of integral() is something suitable for further integration. In particular it tells us that you expect the result of integral() to be a symbolic expression.
integral() cannot be used with symbolic expressions. For symbolic expressions you need to use either int() or vpaintegral() .
You do not have a symbolic expression: you have a character vector. To transform the character vector into a symbolic expression, you will need to use str2sym() (R2018b or later) or sym() (R2018a or earlier)
Also, the bounds that you are fetching are character vectors not numeric. You will need to convert them too.
syms x y z;
Funcion = str2sym(get(handles.EtxtFuncion,'String'));
A1 = str2sym(get(handles.EtxtA1,'String'));
B1 = str2sym(get(handles.EtxtB1,'String'));
A2 = str2sym(get(handles.EtxtA2,'String'));
B2 = str2sym(get(handles.EtxtB2,'String'));
A3 = str2sym(get(handles.EtxtA3,'String'));
B3 = str2sym(get(handles.EtxtB3,'String'));
D1 = str2sym(get(handles.EtxtD1,'String'));
D2 = str2sym(get(handles.EtxtD2,'String'));
D3 = str2sym(get(handles.EtxtD3,'String'));
I1 = int(Funcion, D1, A1, B1);
I2 = int(I1, D2, A2, B2);
I3 = int(I2, D3, A3, B3);
I3_char = char(vpa(I3));
set(handles.ResTotal, 'String', I3_char);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!