I am not good at matlab but I have to write short programs for my exams, so I started to prepare.
3 equations So these must be solved with f solve and I have to display the result of these, but this time I have to start from the origo.
My code is:
x = fsolve(y,x)
function y = t(x)
y=zeros(3,1);
x=ones(3,1);
y(1)=x(1).^3-11*x(1)+x(2).^2+9;
y(2)=x(1)*x(2).^2+x(2)-10*x(1)-11*x(3);
y(3)=x(1)*x(2)+x(3).^4-10*x(2)+8;
end
So as I think my refering of the function is bad. I hope it is and no more mistake in it. The error is: https://imgur.com/a/Slbi1tV
Could you help me out?
Thanking you in advance and have a good health all of you!
Related
General question, if I have MATLAB code looking like this:
for x=0:500
if x <= 250
y = 2*x+1;
else
y = 3*x+2;
end
end
How could I have the same logic using blocks/systems in Simulink? Whenever I try switches and If blocks, I get an error like "Input Ports (1) of __ are involved in the loop.
I can share my model if it is helpful. But if someone could show me how to put this code in terms of a Simulink system, I'm sure I could figure it out. FYI, if it isn't obvious, x is my input and y is my output.
Thank you!
There are several ways of doing it, but check out "If Action Subsystems" for one example of how to do it:
https://se.mathworks.com/help/simulink/slref/ifactionsubsystem.html
I couldn't get Maple to solve this sample function at its own documentation:
rsolve({y(n)=ny(n−1),y(0)=1},y)
The output in the documentation is Gamma(n+1). But when I executed the code, Maple only returns
{y(0) = 1, y(n) = ny(n-1)}
Any help is much appreciated!
You forgot to put multiplication there. Now ny is seen as a variable, and not as n*y. If you insert the mulitplication or a space, then the output is indeed Gamma(n+1)
I currently have the following code:
function v3matrix1 = v3(l,eigendata,sphere)
l0=l;
v3matrix1=zeros(2*l0+1,2*l0+1);
for m0=-l0:1:l0
for m0p=-l0:1:l0
fun=#(theta,phi)conj(Ylm(l0,m0,theta,phi)).*(-gamma11(1,eigendata,sphere,theta,phi)).*partialTheta2(l0,m0p,theta,phi).*sin(theta);
v3matrix1(m0+l+1,m0p+l+1)=integral2(fun,0,pi,0,2*pi);
end
end
end
It takes ~21 mins to run. I have tried to follow similar advice to that given here (Speeding up a double loop over integrals with changing parameter function values in matlab) by getting rid of the for loops using "arrayfun" and "meshgrid" but that didn't speed it up. I was wondering if anyone has any other tips or suggestions that might speed this code up?
Thank you very much.
Ive come a long way with programming as a newbie in Matlab, but now Im really stuck. Ive tried to 'slice' the variable dy, but unfortunately it didnt work. I guess the problem lies in the fact that i is dependent on SACn. Ive done this because the ODE solver needs a vector as input, not a matrix. Can someone please help me?
This is a snippet of code:
parfor SACn=0:SACnum-1
i=19+SACn*200:1:66+SACn*200;
dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:); %#ok<*PFBNS,*PFPIE>
dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
I tried something like this (didnt work though):
parfor SACn=0:SACnum-1
temp1dy=zeros(200,size(y,2));
temp2dy=zeros(200,size(y,2));
for i=19:1:66
temp1dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:); %#ok<*PFBNS,*PFPIE>
temp2dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
dy(SACn*200+1:(SACn+1)*200,:)=temp1dy;
dy(SACn*200+1:(SACn+1)*200,:)=temp2dy;
end
Additional info: I have vectorized my code in an ODE solver of Matlab, these are all the :) 's you see at the end of all variables such as y(i+18-100,:). Ive done this so that the ODE solver computes multiple values of dy for the same time point in parallel and then chooses the best option.
Please do not hesitate to ask for more details!
I know this is a novice question, but I'm new at Matlab and am trying to integrate a function for n=0, n=1, etc.. This is my code so far:
function x = t^n*(t+5)^-1
int(x,t=0..1)
And I keep on getting this error:
Error: File: a02_IX.m Line: 1 Column: 15
Unexpected MATLAB operator.
Does anyone know what this could be?
Thank you!
Try writing it this way:
function x = t^n/(t+5) int(x,t=0..1)
I think raising the term in parentheses to the -1 power reads badly. Maybe MATLAB is confused, too.
What's the value for n? Don't you need to specify that?
It helps to know the answer before you start. This is easy to integrate analytically.
The function looks like this for n=2:
http://www.wolframalpha.com/input/?i=plot+t%5E2%2F%28t%2B5%29+t%3D0..1
Here's the integral, both indefinite and definite:
http://www.wolframalpha.com/input/?i=integrate+t%5E2%2F%28t%2B5%29+from+t%3D0+to+1