MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Jake
on 7 Dec 2012

I am wanting to write a for loop for 2 differential equations.

For 0<x<5 dadb = (2*x) For 5<x<10 dadb = (3*x)

How would I code this?

Cheers

*No products are associated with this question.*

Answer by Rick Rosson
on 9 Dec 2012

Edited by Rick Rosson
on 9 Dec 2012

dx = 0.01; x = (0:dx:10-dx)';

idx = (x < 5);

C = ... D = ...

z = ( C + x.^2 / 2 ) .* idx + ( D + x.^3 / 3 ) .* ( 1 - idx ) ;

You will need to specify values for the constant `C` and `D` based on the initial value of `z` and the continuity condition at `x = 5`.

## 3 Comments

## Walter Roberson

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/56018#comment_115947

Differential equations? So dadb means "the derivative of a with respect to b" ? But the value you give is in terms of a third variable, x ??

## Jake

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/56018#comment_116102

Sorry i will re-write that...

## Jan Simon

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/56018#comment_116104

Are you sure that you mean a FOR-loop? Look at "doc for" and "help for", if this really matches your needs.