CAT arguments dimensions are not consistent.

3 Ansichten (letzte 30 Tage)
John Logan
John Logan am 16 Sep. 2015
Beantwortet: Kirby Fears am 16 Sep. 2015
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']

Antworten (2)

Star Strider
Star Strider am 16 Sep. 2015
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 am 16 Sep. 2015
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.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by