how do i create a matrix with an undefined variable?

조회 수: 26 (최근 30일)
John Kacuba
John Kacuba 2016년 6월 5일
댓글: John D'Errico 2016년 6월 5일
I am trying to create a matrix with the expression "b" in it, without defining a value for "b".
The matrix I want to create is:
0.6b
0
0.8b
Can someone help me with this?
Thank you.

답변 (2개)

John D'Errico
John D'Errico 2016년 6월 5일
편집: John D'Errico 2016년 6월 5일
Very often people want to create a vector like this, without having a good reason for so doing. In fact, recently I've seen people using symbolic tools when they should not have done so, and another case where they used functions for no good reason, where instead a symbolic solution was absolutely called for. Since we don't know why you are asking to solve this problem, there are valid reasons to question the request and to offer an alternative.
So the functional form as cyclist points out is fine, in SOME cases. It creates a vector that is a function of some unknown variable b.
f = @(b)([0.6*b 0 0.8*b])
This might be useful if you are trying to solve a variety of problems, thus root finding, optimization, etc.
If you have the symbolic TB, then of course nothing stops you from creating the vector
syms b
v = [0.6*b, 0, 0.8*b];
Lacking that TB, then you can use my sympoly toolbox, found on the file exchange.
sympoly b
v = [0.6*b, 0, 0.8*b];
HOWEVER, there are some cases where use of symbolic tools such as these examples is ill-advised. It will produce much slower code. If you don't truly need symbolic tools, then don't use them.
  댓글 수: 2
John Kacuba
John Kacuba 2016년 6월 5일
Thank you - this is helpful. Sorry for my ignorance when it comes to MatLab - I'm new at using it and as a result I'm probably not asking very good questions.
John D'Errico
John D'Errico 2016년 6월 5일
It is not a bad question, though a bit incomplete. The issue is to know when to use a given tool, and I will admit, sometimes that may not be obvious to a new user.
For example, you have a problem with an "unknown" in it. Should the problem be symbolic? Or should it be formulated as a solve problem using a numerical tool, thus anything from fzero, fminbnd, fminsearch, fsolve, fminunc, fmincon, etc?
In some cases multiple approaches work. There are always many ways to solve any problem. But is one solution slower than the other? Will you need to solve a similar problem millions of times? Do you need an analytical solution?
So knowing the goal is an important thing to know how best to formulate the problem.

댓글을 달려면 로그인하십시오.


the cyclist
the cyclist 2016년 6월 5일
편집: the cyclist 2016년 6월 5일
You could use an anonymous function:
f = @(b)([0.6*b 0 0.8*b])
Then, for example
f(10)
ans =
6 0 8
  댓글 수: 2
John Kacuba
John Kacuba 2016년 6월 5일
Thank you for the response, but I don't think that's exactly what I'm looking for. I am trying to create this 3x1 vector with 'b' still in the vector undefined so I can then multiply it by a matrix. The issue I'm running into is MatLab wants me to define or assign a value to 'b'. I'd like to just keep 'b' in my vector - [0.6b;0;0.8b].
John D'Errico
John D'Errico 2016년 6월 5일
But you never said that you have the symbolic TB or not!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by