i'm beginner in MATLAB. My problem is :
When i want to take the value of the line in my matrice L(1x2) to stock it in the a and b variables, I tried to use the following code
[a,b]=L;
It didn't work well. I don't understand why it's not possible like this. I used this syntax for the line ginput(n). I used another method to take the value but I want to understand my error in the code above.
My final code is this :
clf();
n=10;
axis([0 10 0 10]);
[px,py] = ginput(n);
Y = py';
X = ones(1,n);
X=[X ; px'];
L= Y*pinv(X);
a = L(1,1);
b = L(1,2);
x = 0:0.2:10;
plot(x,b*x+a, px,py,'r+');
grid;
Thanks a lot
You cannot assign in MATLAB using
[a,b]=L
Instead, you can do the following:
L=[1,2];
L=num2cell(L);
[a,b]=deal(L{:});
Then a=1 and b=2.
Related
I have something similar to the following block diagram on Simulink, which looks rather messy especially with an increasing number of blocks.
I want to replace a 3-point summing block with a function block, while keeping the same output.
First I started by placing the code inside the function block:
function y = fcn(u)
sys1 = tf(0.5,[1 0 0 4]);
sys2 = tf([3 0.5],[1 0 15]);
sys3 = tf(1,[1 1]);
y = sys1 + sys2 + sys3;
However I was greeted with an error saying that Simulink does not support code generation.
"The 'tf' class does not support code generation."
I then came across a similar problem here: https://nl.mathworks.com/matlabcentral/answers/74770-is-there-any-way-to-disable-code-generation-in-simulink
I am trying to implement an extrinsic function or 'wrapper function' with some difficulty. I created a new script called myWrapper.m, containing the same code:
function y = myWrapper(u)
sys1 = tf(0.5,[1 0 0 0 4]);
sys2 = tf([3 5],[1 0 15]);
sys3 = tf(1,[1 1]);
y = sys1 + sys2 + sys3;
and the MATLAB Function edited to:
function y1 = fcn(u1)
y1 = myWrapper(u1);
The error persists.
I somehow want to access myWrapper.m file from the MATLAB Function block. Any pointers on how this should be done? Following the previous link given and the official docs I am ending up with something like this in my MATLAB Function block:
function y1 = fcn(u1)coder.extrinsic('myWrapper')
y1 = myWrapper(u1);
The last code above is syntactically incorrect and I am at a loss on how it should be done. MATLAB automaticaly corrects the above code to:
function y1 = fcn(u1,coder,extrinsic, myWrapper )
y1 = myWrapper(u1);
which is not what I want.
Any tips and/or suggestions on how this could be done would be appreciated.
A similar question was asked on the MathWorks forum here, two years ago, with no response.
I was going about tackling this problem completely wrong. Thanks to several helpful comments I realized that in order to replace the summing block, one must NOT remove the Transfer Function blocks which feed into the summing block.
A MATLAB Function does not support code generation (and rightly so) such that a transfer function may be implemented inside it. That is why the blocks simply feed into the MATLAB Function as follows.
The script would very simply be:
function y1 = fcn(u1, u2, u3)
x = (u1 + u2 +u3);
y1 = x;
end
Hi I'm new to matlab and programming in general and I was wondering if anyone could tell me whats wrong with my code.
These were my instructions and values
Complete the implementation of the dampedOsc function, which evaluates and plots a graph showing the function y=e-0.8xcos 3x at a
set of points spaced at intervals of 0.1 and ranging from 0 to 3π.
The curve should be displayed using a magenta dotted line with
'pentagram' markers.
and my code:
function y = dampedOsc(x)
fplot(#(x) (exp(-0.8*x)*cos(3*x)),[0 3*pi()],'mp:')
end
All tests passed except this one:
Test 1 (Test that function has been plotted):
Actual value does not have correct size:
expectedSize =
1 95
actualSize =
1 98
Test failed.
I honestly have no idea whats gone wrong, I thought it was because of the lack
of point spaced intervals but I have no idea how to input it.
This is an interesting question, so I'll give it a stab. I would probably do something like this.
function z = dampedOsc(x)
if isvector(x) == 0
return
else
z = zeros(1,length(x));
for n = 1 : length(x)
y = exp(-0.8 .* x(n)) .* cos(3 .* x(n));
z(1,n) = y;
end
end
z
end
Then, calling that dampedOsc function from another script, should give the figure.
plot([0 : 0.1 : (3*pi)],dampedOsc(x),'mp:')
I don't have MATLAB in front of me right now, but I think this should work. If it doesn't, I can fix it. Good luck with your project!
I am working on an assignment that requires me to use the trapz function in MATLAB in order to evaluate an integral. I believe I have written the code correctly, but the program returns answers that are wildly incorrect. I am attempting to find the integral of e^(-x^2) from 0 to 1.
x = linspace(0,1,2000);
y = zeros(1,2000);
for iCnt = 1:2000
y(iCnt) = e.^(-(x(iCnt)^2));
end
a = trapz(y);
disp(a);
This code currently returns
1.4929e+03
What am I doing incorrectly?
You need to just specify also the x values:
x = linspace(0,1,2000);
y = exp(-x.^2);
a = trapz(x,y)
a =
0.7468
More details:
First of all, in MATLAB you can use vectors to avoid for-loops for performing operation on arrays (vectors). So the whole four lines of code
y = zeros(1,2000);
for iCnt = 1:2000
y(iCnt) = exp(-(x(iCnt)^2));
end
will be translated to one line:
y = exp(-x.^2)
You defined x = linspace(0,1,2000) it means that you need to calculate the integral of the given function in range [0 1]. So there is a mistake in the way you calculate y which returns it to be in range [1 2000] and that is why you got the big number as the result.
In addition, in MATLAB you should use exp there is not function as e in MATLAB.
Also, if you plot the function in the range, you will see that the result makes sense because the whole page has an area of 1x1.
I have some functions are depending on each ther , the functions are from this book page 136 http://www.cs.helsinki.fi/u/ahyvarin/papers/bookfinal_ICA.pdf .. I functions are presented below , How to write following functions in matlab ??
y(t) = W(t-1)*x(t)
h(t) = P(t-1)*y(t)
P(t)=(1/B)*Tri[P(t-1)-m(t)*h^T(t)]
m(t) = h(t)/(B+y^T(t))*h(t))
e(t) = x(t)-W^T(t-1)*y(t)
W(t) = W(t-1) + m(t)*e^T(t)
It is solving the weight matrix W(t) iteratively .. I tried to do like this in matlab but I did not work so may be you can advice to correct the code :
for i=1:10
e=randn(3,5000);
A=[1 0 0;-0.5 0.5 0;0.3 0.1 0.1];
x=A*e;
y(t) = W(t-1)*x(t)
h(t) = P(t-1)*y(t)
P(t)=(1/B)*Tri[P(t-1)-m(t)*h^T(t)]
m(t) = h(t)/(B+y^T(t))*h(t))
e(t) = x(t)-W^T(t-1)*y(t)
W(t) = W(t-1) + m(t)*e^T(t)
end
Thanks
Ok. I can't really understand what you want, but your code shows that you don't understand some moments. I will try to clarify some moments to you:
for i = 2:10
x = rand(3);
y = W(:,:,i-1)*x;
h = P(:,:,i-1)*y;
m=h/(1+y'*h);
P(:,:,i)=P(:,:,i-1)*m*h';
e=x-W(:,:,i-1)'*y;
W(:,:,i)=W(:,:,i-1)+m*e';
end
You must go something like this: 1. you calculate x and use it to calculate other functions.
2. all of them are matrices. So you need to define it first. For example y = ones(3) etc. 3.Thats not y^T or e^T. Its transposing. If you do not feel difference it's early for you to solve this task :)
And the last: Tri function will create a some kind of problems to you, but it's defined at 136 page.
P.S. i missed beta becouse of don't know what is it :)
I want to plot the double integral
using MATLAB for s between 0 and 1. How can I do this?
I've tried the following code but since this is my first time using MATLAB it didn't worked after two hours:
syms r x s
fun=#(r,x)(r^8*sin(x)^8)/((r*sin(x)+1)^7)+....
f=int(fun,x,0,2*pi)
q=int(f,r,0,1)
Thanks for your help!
You can use function dblquad for each value of s on interval.For example(other parts add the feature yourself):
Int = zeros(1,101);
F = #(x,r) (r.^8.*(sin(x).^8)./(r.*sin(x)+1).^7);
for s = 1:101
Int(s) = dblquad(F,0,(s-1)*0.01,0,2*pi);
end
plot(0:0.01:1,Int)