CAT arguments dimensions are not consistent.

3 ビュー (過去 30 日間)
John Logan
John Logan 2015 年 9 月 16 日
回答済み: Kirby Fears 2015 年 9 月 16 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by