在MATLAB中衍生gui? [英] Derivative in MATLAB gui?

查看:138
本文介绍了在MATLAB中衍生gui?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算一个编辑文本框(edit1)的派生值,并在静态文本框(text1)中显示答案。但它只是显示数字。我做错了什么?

 %---在按钮按钮1上执行按钮。 
函数pushbutton1_Callback(hObject,eventdata,句柄)
%pushbutton1的对象句柄(见GCBO)
%eventdata保留 - 在将来版本的MATLAB中定义
句柄结构带有句柄和用户数据(参见GUIDATA)
x = -10:.1:10;
equation = get(handles.edit1,'String');
y = eval(equation);
derive_func = diff(y);
set(handles.text1,'String',derive_func);
plot(y);

GUI Image -
正如您所看到的,它绘制了该函数,但在尝试区分时会返回3行数字:

diff 的两种不同用法之间存在冲突。默认的内置使用是数字区分,并且您正在将该函数应用于数字变量 y ,以便获得数字输出。

您似乎想要做的是使用符号数学工具箱中的 diff 来显示 5 * x ^ 4 ,这就要求你告诉matlab你要使用符号数学工具箱,通过提供正确的输入diff,通常是一个字符串。



我正在使用matlab R14,并且在新版本的sym工具箱中进行了很多更改,但以下内容适用于您。

  str ='x ^ 5'; 
diff(str,'x')

其中 str 是您想要象征性地区分的表达式。请注意,在我的版本中,sym工具箱对符号 x。^ 5 并且倾向于 x ^ 5 ,I不知道它如何在MuPad上工作,但您可能必须找到一种解决方法,以确保您使用它可以处理的字符串来馈送MuPad(或您正在使用的任何一种sym引擎)。

编辑



有关使用 cd addpath 来控制使用哪个版本的重载函数 diff 已被删除。

I'm trying to calculate the derivative of a an edit text box (edit1) and display the answer in a static text box (text1). But it is just displaying numbers. What am I doing wrong?

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=-10:.1:10;
equation = get(handles.edit1, 'String');
y = eval(equation);
derive_func = diff(y);
set(handles.text1, 'String', derive_func);
plot(y);

GUI Image - As you can see, it plots the function, but returns with 3 lines of numbers when it tries to differentiate:

解决方案

You are seeing a conflict between two different uses of the overloaded function diff. The default builtin use is numeric differentiation, and you are applying the function to numeric variable y so you are getting numeric output.

What you seem to want to do is use diff from the symbolic math toolbox to diplay 5*x^4 and that requires that you tell matlab that you want to use the symbolic math toolbox by providing diff with the right input, usually a string.

I am using matlab R14 and a lot has changed in the sym toolbox with newer versions, but the following should work for you.

str = 'x^5';
diff(str,'x')

where str is the expression you want to differentiate symbolically. Note that in my version the sym toolbox is unhappy with the notation x.^5 and prefers x^5, I don't know how it might work on MuPad, but you may have to find a workaround to make sure that you feed MuPad (or whichever sym engine you are using) with a string it can handle.

edit

Earlier suggestions on the use of cd or addpath to control which version of overloaded function diff is used have been removed.

这篇关于在MATLAB中衍生gui?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆