Dimension too large error with small number - tikz

I get a "Dimension too large" error because of \tmp but I don't understand why, because the result is not that large (around 1).
\begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-0.10000, xscale=0.10000, inner sep=0pt, outer sep=0pt]
\def\xxpl{834.5599}
\def\yxpl{-46.1934}
\def\xxpr{1567.4944}
\def\yxpr{723.6958}
\def\tmp{(\yxpr-\yxpl)/(\xxpr-\xxpl)}
\draw[black] (0,0) -- ++({10*\tmp},{-1*10}); % Works
\draw[black] (0,0) -- ++({100*\tmp},{-1*100}); % Doesn't work
\end{tikzpicture}
Do you know why ?
Thank's in advance

With \def, the expression defined is just replace in the subsequent code. Unfortunately, it looks like tikz cannot compute a complex expression such as (\yxpr-\yxpl)/(\xxpr-\xxpl). You can use \pgfmathsetmacro instead of \def:
\begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-0.10000, xscale=0.10000, inner sep=0pt, outer sep=0pt]
\def\xxpl{834.5599}
\def\yxpl{-46.1934}
\def\xxpr{1567.4944}
\def\yxpr{723.6958}
\pgfmathsetmacro\tmp{(\yxpr-\yxpl)/(\xxpr-\xxpl)}
%\draw[black] (0,0) -- ++({10*\tmp},{-1*10}); % Works
\draw[black] (0,0) -- ++({100*\tmp},{-1*100}); % Now work!
\end{tikzpicture}

Related

'pie' function in MATLAB gives "undefined function 'cos'" error

I wrote a function, wins_plot, to read the scoreboard from a file and store the player's name, number of plays, wins, & losses. I stored all those using struct. I loop over the file, store each line in line, textscan for everything I need from line, and then iterate i (initially == 1) as I go to expand my array of structures. A snippet from the code to represent what I am saying:
c = textscan(line, '%s %s %d %d %d');
player(i).firstName = c{1};
player(i).lastName = c{2};
player(i).plays = c{3};
player(i).wins = c{4};
player(i).losses = c{5};
After all the file has been scanned and stored, I then write this code to extract the number of wins of each player and store it in X and then finally use the pie function to represent the values in X
for n=1:(i-1)
X(n) = player(n).wins;
end
pie(X);
I get a wall of error after:
Undefined function 'cos' for input arguments of type 'int32'.
Error in pol2cart (line 22) x = r.*cos(th);
Error in pie (line 99)
[xtext,ytext] = pol2cart(theta0 + x(i)*pi,1.2);
Error in wins_plot (line 30) pie(X);
I have no clue what might be wrong. Any help would be greatly appreciated. Please keep in mind that I only just started learning MATLAB today so my knowledge of it is very limited (and I have R2013a). Thank you in advance!
The numbers got read as int32, but when you call pie, it requires them to be double to do the computation. So, when you call pie, try casting the values to double. Try this,
pie(double(X));

Summation of piecewise functions in matlab

I have function y, whish is the sum of piecewise functions f0_basis and f1_basis (defined on [r_i-1; r_i+1]), multipled by number coefficients alpha and beta:
y=#(r) 0;
for j=1:1:N
y1= #(r) f0_basis(r ,j).*alpha(j)+f1_basis(r ,j).*beta(j);
y=#(r) y1(r)+y(r);
end
fplot(y,[a,b])
The fplot gives me what I want (a=1 and b=1.1):
However when I try, to find the value of y(r_i) - it always gives me the same wrong result for (a<r_i<b)
y(r_i)=-0.0016
which doesnt reperesents the plot above. Though if I try y(a) and y(b) - it gives me the right result.
What is wrong? I want to simply create an array y_array(i) = y(r_i).
N=11
a=1; b=1.1
delta_r=(b-a)/(N-1)
r_grid(i)=a+delta_r.*(i-1);
function y=f0_basis(r ,i)%i - номер середины узла
ri=r_grid(i);
r1=ri-delta_r;
r2=ri+delta_r;
y1=(2*((r-ri)/delta_r).^3-3*((r-ri)/delta_r).^2+1).*(r>=ri & r<=r2 & r2<=b);%правый конец
y2=(-2*((r-r1)/delta_r).^3+3*((r-r1)/delta_r).^2).*(r>=r1 & r<=ri & r1>=a);%левый конец
y3=0;%в сотальных случаях
y=y1+y2+y3;
end
function y=f1_basis(r ,i)%ri - середина узла
ri=r_grid(i);
r1=ri-delta_r;
r2=ri+delta_r;
r_div_1=(r-ri)/delta_r;
r_div_2=(r-r1)/delta_r;
y1=delta_r*(r_div_1.^3-2*(r_div_1.^2)+r_div_1).*(r>=ri & r<=r2 & r2<=b);%правый конец
y2=delta_r*(r_div_2.^3-r_div_2.^2).*(r>=r1 & r<=ri & r1>=a);%левый конец
y3=0;%в сотальных случаях
y=y1+y2+y3;
end
Alpha:
-0,000828144527478242
-0,000823822186226137
-0,000819532164240625
-0,000815314580859248
-0,000811187867518122
-0,000807162471718638
-0,000803245019928998
-0,000799439804012876
-0,000795750103056819
-0,000792175886650016
-0,000788726122705251
Beta:
-1,14781261976785e-05
-0,00257727396861173
-0,000627562853139588
0,000211869596648955
0,000683549308474220
0,000987694942326156
0,00119967671089174
0,00136043282028271
0,00145954455875083
0,00163805699746171
-0,000147024199470849
UPDATE:
it seems that y(r_i)=-0.0016 for only r_grid(i) array. If I try some r not from r_grid(i) array - it works fine.
I think your problem is in the functions f0_basis and f1_basis. You compute 2 terms inside each function (the 3rd one is null), each term is subject to some constraints. But the constraints are overlapping for one point, if r==ri. In this case, you are adding two equal terms, but nowhere else. That's why you have strange results at r_grid(i).
So change the tests, for example r>=r1 & r<ri & r1>=a for y2.

How can I plot these variables in Matlab?

I want to plot my variables for teta=0:360 degree. But I cant solve matrix problems. Plot all variables such as vc, ac, bzegon ,bdot.
teta=0:.1:2*pi;
w=2000*2*pi/60;l1=.05;l2=0.15;
beta=asin(l1/l2*sin(teta));
bdot=l1/l2*w*(cos(teta)./sin(beta));
xc=l1*cos.(teta)+l2*cos.(beta);
vc=-l1*w*sin.(teta)-l2*bdot.*sin.(beta);
bzegon=-l*w*(w*sin.(teta)*(xc-l1*cos.(teta))+cos.(teta)*(vc+l1*sin.(teta)))/((xc-l*cos. (teta))^2);
ac=-l1*w*cos.(teta)-l2*bzegon.*sin.(beta)-l2*bdot.*cos.(beta);
plot(teta,bdot);
set(gca,'XTick',0:pi/2:2*pi)
set(gca,'XTickLabel',{'0','pi/2','pi','3pi/2','2p'})
%title('bdot');
ylabel('bdot');
xlabel('radian');
replace:
xc=l1*cos.(teta)+l2*cos.(beta);
vc=-l1*w*sin.(teta)-l2*bdot.*sin.(beta);
bzegon=-l*w*(w*sin.(teta)*(xc-l1*cos.(teta))+cos.(teta)*(vc+l1*sin.(teta)))/((xc-l*cos. (teta))^2);
ac=-l1*w*cos.(teta)-l2*bzegon.*sin.(beta)-l2*bdot.*cos.(beta);
with:
xc=l1*cos(teta)+l2*cos(beta);
vc=-l1*w.*sin(teta)-l2*bdot.*sin(beta);
bzegon=-l1*w.*(w.*sin(teta).*(xc-l1*cos(teta))+cos(teta).*(vc+l1*sin(teta)))/((xc-l1*cos(teta)).^2);
ac=-l1*w.*cos(teta)-l2*bzegon.*sin(beta)-l2*bdot.*cos(beta);
You have to check all the parameters once again, maybe I messed up somewhere.
There are a lot of mistakes in your code with the . operator. You can't do cos.(beta), the . operator for element-by-element operation needs to be before things like multiply, add, subtract, raise to the power n, etc... Here's a corrected version of your code (I have assumed a scalar value for l which is not specified in your question):
teta=0:.1:2*pi;
w=2000*2*pi/60;l1=.05;l2=0.15;
l=l1+l2; % or whatever value, I assumed l was a scalar
beta=asin(l1/l2*sin(teta));
bdot=l1/l2*w*(cos(teta)./sin(beta));
xc=l1*cos(teta)+l2*cos(beta);
vc=-l1*w*sin(teta)-l2*bdot.*sin(beta);
bzegon=-l*w.*(w.*sin(teta).*(xc-l1*cos(teta))+cos(teta).*(vc+l1*sin(teta)))/((xc-l*cos(teta)).^2);
ac=-l1*w.*cos(teta)-l2*bzegon.*sin(beta)-l2*bdot.*cos(beta);
plot(teta,bdot);
set(gca,'XTick',0:pi/2:2*pi)
set(gca,'XTickLabel',{'0','pi/2','pi','3pi/2','2p'})
%title('bdot');
ylabel('bdot');
xlabel('radian');

troubleshooting MATLAB Iteration code

I have an iteration code which I am using to find latitude/longitude of a set of heights (h_intercept). This is a 1x79 matrix.
It works perfectly until the 22nd value. I've found that this is when h_test>h_intercept. I tried to put a condition in to reset it but it doesn't work.
When h_test>h_intercept, all of the range values become zero
For example
for j=20:40
rng_sat= sat_look_tcs_pass1(3,j);
u_sat=[sat_look_tcs_pass1(1,j)/sat_look_tcs_pass1(3,j);sat_look_tcs_pass1(2,j)/sat_look_tcs_pass1(3,j);sat_look_tcs_pass1(3,j)/sat_look_tcs_pass1(3,j)];
h_intercept=sat_look_pass1_llh(3,j)/2e3;
h_test=zeros(1,3);
rng_test_min=0;
rng_test_max=rng_sat/2e3;
err=0.01;
while abs(h_intercept-h_test)>err
rng_test=(rng_test_min+rng_test_max)/2;
tcs_test=u_sat*rng_test;
llh_test=tcs2llhT(tcs_test,station_llh);
h_test=llh_test(3,:);
if h_test>=h_intercept
rng_test_max=rng_test;
else
rng_test_min=rng_test;
end
end copter_llh(:,j)=(llh_test); h_interceptloop(:,j)=(h_intercept); end % code end
Any suggestions appreciated!
I think the error is in the first loop. In the first line you choose values 60 to 79:
h_intercept=sat_look_pass1_llh(3,60:79)/2e3;
However, you only use the length of that vector and not its values in the following code.
You iterate over the length of h_intercept, i.e. from 1 to 19:
for j=1:length(h_intercept)
which means that h_intercept=sat_look_pass1_llh(3,j)/2e3; will get the wrong value, as j ranges from 1 to 19 and not from 60 to 79.
If you change your for loop to for j=60:79, it should work ( and you can also delete the first line h_intercept=sat_look_pass1_llh(3,60:79)/2e3; )

Nested if statement in for loop

So heres my script
function printPower
sum=0;
filename=input('Enter a filename: ','s');
power=load(filename);
for i=1:length(power);
if power(i)>=0;
sum=sum+power(i);
end
TP=sum/24;
end
fprintf('Total power: %.1f kWh.\n', TP);
There are negative values in the text file im loading and I want it to only sum the positive ones but it still sums all values.
You could just replace your loop with something like
total = sum(power(power>=0))/24
Personally I think that using the name of a Matlab intrinsic function, such as sum, as a variable name is just asking for trouble though I'm not sure it's caused a problem in your case. That's why the lhs of my statement is the variable total.