Question

4. Using inbuilt function in MATLAB, solve the differential equations: dx --t2 dt subject to the condition (01 integrated fro

Matlab Code for these please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The matlab codes for both questions are given below.

Q 4. ******************script1.m*************************


Analytical_Sol=@(t)t.^2-exp(-t)-2*t+2;

dxdt=@(t,x)-x+t^2;
x0=1;
%Numerical Solution
[t,sol] = ode45(dxdt, [0, 2], x0);

%Analytical solution
y=Analytical_Sol(t);
err=abs(sol-y);
fprintf('Numerical\t\tAnanlytical\n');
for i=1:length(t)
fprintf('%f\t\t%f\n',sol(i),y(i));
end

Q5. *******************************script2.m**********************************


clc;
clear all

alpha=1;
beta=0.05;
u=0.02;
n=0.5;
x0=10;
y0=10;
%f(1) represent dx/dt and f(2) represent dy/dt
func = @(t,f) [alpha*f(1)-beta*f(1)*f(2);u*f(1)*f(2) - n*f(2)];


[t,sol] = ode45(func, [0, 20], [x0,y0]);
subplot(211)
%sol(:,1) contain prey population
plot(t,sol(:,1))
hold on

%sol(:,2) contain predator
plot(t,sol(:,2))
legend('prey','predator')
hold off

%Now vary mu and see the change
y=0.01:0.01:0.1;
subplot(212)
for i=1:length(y)
u=y(i);
func = @(t,f) [alpha*f(1)-beta*f(1)*f(2);u*f(1)*f(2) - n*f(2)];
[t,sol] = ode45(func, [0, 20], [x0,y0]);
plot(t,sol(:,1))
hold on
plot(t,sol(:,2))
end
title('changing mu')
hold off

Add a comment
Know the answer?
Add Answer to:
4. Using inbuilt function in MATLAB, solve the differential equations: dx --t2 dt subject to the ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT