Why do I get the error Undefined function or variable 'f1_2'?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Minoli Fernando
il 14 Apr 2023
Commentato: Minoli Fernando
il 15 Apr 2023
%define space from 0 to 2
x=linspace(0,2,11);
lambda=0.1;
%initial guess for the answer
guess= bvpinit(x,[1;0;0;0;1;0;1;0]);
%setting options
options = bvpset('Stats','on');
for i = 1:4
%calling bvp4c to solve the boundary value problem
sol=bvp4c(@bvp_func_3,@boundary_conditions,guess,options,lambda);
%export solution
f = sol.y;
if lambda==0.1
f1_1=f(1,:);
f2_1=f(2,:);
f5_1 = f(5,:);
X_1=sol.x;
else if lambda==0.3
f1_2=f(1,:);
f2_2=f(2,:);
f5_2=f(5,:);
X_2=sol.x
else if lambda==0.5
f1_3=f(1,:);
f2_3=f(2,:);
f5_3 = f(5,:);
X_3=sol.x;
else if lambda==0.7
f1_4=f(1,:);
f2_4=f(2,:);
f5_4 = f(5,:);
X_4=sol.x;
end
end
end
end
lambda=lambda+0.2;
end
lambda=lambda+0.1
sol=bvp4c(@bvp_func_3,@boundary_conditions,guess,options,lambda);
f = sol.y;
f1_5=f(1,:);
f2_5=f(2,:);
f5_5 = f(5,:);
X_5=sol.x;
%plotting the solutions of f with magentic parameter
figure
plot(X_1,f1_1,X_2,f1_2,X_3,f1_3,X_4,f1_4,X_5,f1_5)
legend('lambda=0.1','lambda=0.3','lambda=0.5','lambda=0.7','lambda=1')
xlabel('neta')
ylabel('f')
grid on
0 Commenti
Risposta accettata
Walter Roberson
il 14 Apr 2023
format long g
lambda = 0.1;
fprintf('0.1 exactly expected, but got %.99g\n', lambda);
lambda = lambda + 0.2;
fprintf('0.3 exactly expected, but got %.99g\n', lambda);
if lambda == 0.3
fprintf('0.3 literal compared to lambda, bit-for-bit comparison is exactly equal!\n');
else
fprintf('0.3 literal compared to lambda, bit-for-bit comparison is different!\nDifference is %.99g\n', lambda - 0.3);
end
Your code assumes that however the literal number 0.1 is represented, that if you add the representation of the literal number 0.2 then you will get exactly (bit for bit) the same as the representation of the literal number 0.3 .
I want you to consider for a moment, suppose that MATLAB internally used decimal representation. Suppose it used 16 digit decimal (has to be a finite number of digits). Then suppose you took
A = 1/3 --> hypothetical internal representation 0.3333333333333333
B = 2/3 --> hypothetical internal representation 0.6666666666666666
The hypothetical internal decimal representation of 2/3 being exactly twice the internal decimal representation of 1/3.
Now consider, what happens if you calculate
A + B --> 0.3333333333333333 + 0.6666666666666666 = 0.9999999999999999
which is not 1. So if you were to use finite decimal representation for your calculations, you would find that the representation for 1/3 plus the representation for 2/3 is not exactly the representation for 3/3. And clearly if you were to use another 50 or 792 decimal places, exactly the same problem would occur.
In any "base" numeric system that uses a finite number of digits -- whether base 2, base 10, base 60, base 792 -- there will always be such situations arising. The reciprical of any number that is mutually prime with the base of calculation will always require an infinite number of digits for precise representation. If you truncate to any finite number of digits and round down, then if you add enough of the resulting value together then you will get a result that is smaller than you would expect algebraically. If you truncate to any finite number of digits and round up, then if you add enough of the resulting value together then you would get a result that is larger than you would expect.
Base 2 is fundamentally unable to represent exactly 1/10 in any finite number of bits. Add enough of the approximations together and you are going to get a value you would not expect algebraically.
This is not a bug in MATLAB: this is a fundamental mathematical limitation of working with finite numbers of places in any numeric base.
So... don't do that !!
Do not add floating point values together and expect the result to be exactly a particular literal value. (You can get away with it in binary floating point if you are adding 0.5's, but it is a bad practice to get into.)
You should instead calculate
lambda = (2*i - 1)/10;
and compare i values to see if you are at particular breakpoints.
if i == 1
...
elseif i == 2
...
end
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Physical Units in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!