How do I pass an equation as a parameter to a class constructor?

5 ビュー (過去 30 日間)
Eman Ahmed Elsayed
Eman Ahmed Elsayed 2011 年 5 月 31 日
I want to make method takes an equation as input and then differentiate it and substitute with x=0,1 in the output equation
When I try to run my code I get this error:
>> aa=f(2x+1)
??? aa=f(2x+1)
|
Error: Unexpected MATLAB expression.
Is this because the constructor doesn't accept any equation?
Here is my code:
classdef SAA
%UNTITLED5 Summary of this class goes here
% Detailed explanation goes here
properties
x
equation
end
methods
function f=SAA(e)
f.equation=e;
end
function [value_1,value_2]=differntiate(~)
dydx=diff(equation,x)
% I can't complete the problem and substitute
end
end
end
any help will be appreciated.
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 5 月 31 日
Was this issue solved for you by your other question, http://www.mathworks.com/matlabcentral/answers/8528-how-can-i-pass-an-equation-as-input-parameter-to-function ?
(And if so, how are you doing the differentiation if what you are handed is a function handle?)

サインインしてコメントする。

回答 (4 件)

John D'Errico
John D'Errico 2011 年 5 月 31 日
What can't you complete? Is part of your problem that you spelled it incorrectly as differntiate in the classdef? Then when you spell it properly when you call it, it must fail.
Or is the problem that you don't know how to use subs?
help subs
Or is the problem that you are trying to differentiate equation, rather than f.equation?
Why did you feel the need to make x a property anyway?
  4 件のコメント
John D'Errico
John D'Errico 2011 年 5 月 31 日
As Paulo says, matlab requires * when you wish to multiply. Why are you writing this as a class anyway? That is truly using a Mack truck to carry a pea to Boston. And when you write f(2*x+1), where did f come from???
Eman Ahmed Elsayed
Eman Ahmed Elsayed 2011 年 5 月 31 日
when I write
f=SAA(2)// it's works well
but when I write
f=SAA(2*x+1)
the compiler said
??? Undefined function or variable 'x'.
Please, How can I fix this problem

サインインしてコメントする。


Walter Roberson
Walter Roberson 2011 年 5 月 31 日
What data type is your equation? diff() only differentiates if the data type is "symbol" from the symbolic toolbox.

Todd Flanagan
Todd Flanagan 2011 年 5 月 31 日
You wouldn't pass something like 2x + 1 to DIFF. DIFF expects a vector or matrix of coefficients. In your case something like:
>> aa=f([2 1])
  1 件のコメント
John D'Errico
John D'Errico 2011 年 5 月 31 日
Um, if the input is symbolic, then diff acts "diff"erently.

サインインしてコメントする。


Eman Ahmed Elsayed
Eman Ahmed Elsayed 2011 年 5 月 31 日
when I write f=SAA(2)// it's works well but when I write f=SAA(2*x+1) the compiler said ??? Undefined function or variable 'x'. Please, How can I fix this problem
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 5 月 31 日
If you are going to use diff() to differentiate then your input needs to be a sym() object created by the symbolic toolkit.

サインインしてコメントする。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by