CAT arguments dimensions are not consistent.

9 次查看(过去 30 天)
Why am I getting this error:
>> fpi1
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> fpi1 at 17
[x' g(x)' e' rat']
I want to output all of the 4 values and x and g(x) would help me to see the roots.
%Program 1.2 Fixed-Point Iteration
%Computes approximate solution of g(x)=x
%Input: inline function g, starting guess x0,
% number of steps k
%Output: Approximate solution xc
g=@(x) (5-3*x.^3)/(11.*x+11);
x(1)=1;steps=20;
for i=1:steps
x(i+1)=g(x(i));
end
r=x(steps+1);
e=x-r;
for i=1:steps
rat(i+1)=e(i+1)/e(i);
end
rat(1)=0;
[x' g(x)' e' rat']

回答(2 个)

Star Strider
Star Strider 2015-9-16
You need to do element-wise division as well. See if this change solves the problem:
g=@(x) (5-3*x.^3)./(11.*x+11);

Kirby Fears
Kirby Fears 2015-9-16
Your last statement is attempting to combine arrays x, g(x), e, and rat into a single array. To combine the arrays correctly, you have to do so over a dimension that is the same size in each array.
If you remove the transpose operators, the last line becomes
[x g(x) e rat]
and this works fine. All of these arrays have a size of 1 in dimension 1 (e.g. they have 1 row each).
I'm not sure this is the operation you were intending to perform. If you're trying to store all of these values into a single structure to pass somewhere else, you can combine them into a struct.
output.x=x;
output.gx=g(x);
output.e=e;
output.rat=rat;
On a side note, you should not use "e" as a variable name because it's generally associated with the mathematical constant e. Also, 1e10 in Matlab means 1*10^10. You don't want any ambiguity in your code.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by