Why can't I write this row vector?

1 次查看(过去 30 天)
I simply want a 1x3 row vector like this: . I tried the following:
[x(1,3); x(2,3); x(3,3)]
but I got the following error:
Unrecognized function or variable 'x'.
Why? How to sort it out?

采纳的回答

John D'Errico
John D'Errico 2020-3-22
编辑:John D'Errico 2020-3-22
Very often, people who are just starting to use MATLAB, think that if you don't know the value of something, then it must be symbolic. That is not always true. I'll give some examples later. But to do what you are asking right now, just do this:
X = sym('X',[1,3])
X =
[X1, X2, X3]
As I said, you can have an unknown that will then be estimated. For example, if we stay in the purely numerical domain, we can solve for an unknown vector. This eample is pretty simple, but...
fun = @(X) sum((X - [1 2 3]).^2);
Xinit = [4 4 4];
Xfinal = fminsearch(fun,Xinit)
Xfinal =
0.99999144502901 1.99997109582809 2.99995541073201
Of course, the minimum arises at the vector [1 2 3].
  1 个评论
Armando MAROZZI
Armando MAROZZI 2020-3-22
Thanks a lot for the explanation John. As you mention that example, I grab the opportunity to ask you another question. I am solving a symbolic non-linear system of equations. I did all the steps correctly I think but when I run:
x = fsolve(fun, x0, option)
I get:
fsolve stopped because the sum of squared function values, r, has gradient with
relative norm 1.490116e-08; this is less than options.OptimalityTolerance = 1.000000e-06.
However, r = 3.000000e+00, exceeds sqrt(options.FunctionTolerance) = 1.000000e-03.
As far as I understand it, I shall change "option", right? At the moment is:
Default properties:
Algorithm: 'trust-region-dogleg'
CheckGradients: 0
FiniteDifferenceStepSize: 'sqrt(eps)'
FiniteDifferenceType: 'forward'
FunctionTolerance: 1.0000e-06
MaxFunctionEvaluations: '100*numberOfVariables'
MaxIterations: 400
OptimalityTolerance: 1.0000e-06
OutputFcn: []
SpecifyObjectiveGradient: 0
StepTolerance: 1.0000e-06
TypicalX: 'ones(numberOfVariables,1)'
UseParallel: 0
I'll edit it and re-run the code?

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2020-3-22
Define x somehow before that line, like these examples
x = magic(3)
x = rand(3)
x = randi(100, 3, 3)
or whatever.
  3 个评论
Image Analyst
Image Analyst 2020-3-22
编辑:Image Analyst 2020-3-22
I don't have that toolbox so I don't know what kind of assumptions it makes. I've added it to the product list, which you forgot to do when you created this posting.
What are you going to do after that? How are you going to use x? Or that 3 element, 1-D column vector you're trying to make from the 2-D x matrix?
Armando MAROZZI
Armando MAROZZI 2020-3-22
sorry I am moving from R to Matlab and I make silly mistakes. I'm writing a function to solve a non-linear system of equations. I think it's easier for me to write the row vector directly in the script and not trying to save it in the original command section

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by