How can I plot these variables in Matlab? - 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');

Related

How to make a two-dimensional matrix w/o for?

I'm coding the matrix for the 2-dimensional graph, now.
Although it's so simple equation, it takes a lot of time for performing. I think it could get faster.
especially, "for - command term" could be simplified I think.
How can I simplify this?
q=1:1:30
x(q)=330+q*0.3
F=1:30:8970
T=x(1)-0.3:0.001:x(30)+0.3
n=size(T,2)
k=1:1:n
for a=1:1:30
I(a,k)=F(a)*exp(-2.*(T(:,k)))
end
happy=sum(I)
plot(k,I)
I would say that the time is used to print results. Try to use ; at the end of each line, it will fasten computation.
You can also replace the for loop by the following element by element computation:
a = (1:1:30).';
aux = repmat(exp(-2.*(T(:,k))), length(a), 1);
a = repmat(a, 1, length(k));
I = a.'.*aux.';

vectorize a function with multiple variables

Consider the arbitrary function:
function myFunc_ = myFunc(firstInput, secondInput)
myFunc_ = firstInput * secondInput;
end
Now imagine I want to map the above function to an array for the first input firstInput, while the second input secondInput is constant. For example, something like:
firstVariable = linspace(0., 1.);
plot(firstVariable, map(myFunc, [firstVariable , 0.1]))
where 0.1 is an arbitrary scalar value for the secondInput and firstVariable array is an arbitrary array for the firstInput.
I have looked into the arrayfun() function. However, I don't know how to include the constant variable. Plus it seems like the syntax between MATLAB and Octave are different, or maybe I'm mistaken. It is important for me to have a cross-compatible code that I can share with colleagues.
Assuming in the original function you were multiplying two scalars and you want to vectorise, then
function myFunc_ = myFunc(firstInput, secondInput)
myFunc_ = firstInput .* secondInput;
end
should work just fine.
Then plot it directly:
plot( firstVariable, myFunc(firstVariable , 0.1) )
I'm afraid the arbitrary examples given in the original question were too simplified and as a result, they do not represent the actual issue I'm facing with my code. But I did manage to find the right syntax that works inside Octave:
plot(firstVariable, arrayfun(#(tempVariable) myFunc(tempVariable, 0.1), firstVariable))
basically the
#(tempVariable) myFunc(tempVariable, 0.1)
creates what is so-called an anonymous function and the
arrayfun(<function>, <array>)
maps the function over the given array.

Matlab function number of array element

I would be happy to hear somebody explain me the meaning of 1: before the function below please.
yActual = data2_test.Response;
x = 1:numel(yActual)';
A single colon operator in the context of your snippet is a short-hand way of creating a vector:
start_number:end_number
equivalent to
[start_number, start_number+1, start_number+2, ... end_number-1,end_number]
In your specific case, the variable x is a vector from 1,2,3... up to the number of elements in data2_test.Response.

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.

Using loop in fsolve and plugging the result to a function

I have functions which has totally 4 unknowns (2x2).
I got the solution. But what I am going to do is to vary some parameters so that I can see how the original solution changes.
But matlab keeps saying that in A(I)=B, 'I' and the number of element B must be the same.
Example code could be such as( in the code, psi01 psi02 psi03 psi04 are the varying parameters);
R=902;
psi01=0.9:-0.1:0.1;
psi11=0.9:-0.1:0.1;
psi02=0.1:0.1:0.9;
psi12=0.1:0.1:0.9;
E0=[R/4 R/4; R/4 R/4];
for i=1:9
vv=#(E) [
g/E(2,1)-(th1+psi12(i)/psi11(i)*th2)-((psi01(i)/psi11(i))+a*b)/b*(g/E(1,1)-(rho*b* (psi11(i)/psi01(i))/(psi01(i)/psi11(i)+a*b))*(th1+psi12(i)/psi11(i)*th2)*(th1+psi02(i) /psi11(i)*th2) );
g/E(2,2)-(psi11(i)/psi12(i)*th1+th2)-((psi02(i)/psi12(i))+a*b)/b*(g/E(1,2)-(rho*b*(psi12(i)/psi02(i))/(psi02(i)/psi12(i)+a*b))*(psi11(i)/psi12(i)*th1+th2)*(psi01(i)/psi02(i)*th1+th2) );
R-(E(1,1)+E(1,2)+E(1,1)+E(2,2));
E(1,2)- c(E)*E(1,1);
E(2,2)- d(E)*E(2,1)
];
Ep2(i)=fsolve(vv, E0);
end
If the result of fsolve is a 2x2 array, then how can you expect to save it as a scalar?
Ep2(i)=fsolve(vv, E0);
Ep2 is being used here to store a single element, a scalar. You can't stuff 4 elements into one, and not get an error.
Use a 3-dimensional array, a struct, or use a cell array.